duanmei1536 2015-06-03 17:45
浏览 45
已采纳

无法读取数组中的XML子元素

I am trying to read this xml content http://auroserver.com/sandbox/xml.xml as arrays using PHP

i've read some QA here but still cannot implement how to read these file completely.

I am using this code:

$url            = 'http://auroserver.com/sandbox/xml.xml';
$fileContents   = file_get_contents($url);
$fileContents   = str_replace(array("
", "", "\t"), '', $fileContents);
$fileContents   = trim(str_replace('"', "'", $fileContents));
$Xml            = simplexml_load_string($fileContents);
$xml_array      = unserialize(serialize(json_decode(json_encode((array) $Xml), 1)));
var_dump($xml_array);

using above code, it only return:

array(3) {
  ["Status"]=>
  string(1) "1"
  ["Message"]=>
  array(0) {
  }
  ["Content"]=>
  array(0) {
  }
}

how to render all xml child values? for this things i need to read these values in the xml file:

<CardNo>6048180000002031</CardNo>
<Status>true</Status>
<Ebalance>92900</Ebalance>
<RewardPoint>6900</RewardPoint>

thank you

  • 写回答

1条回答 默认 最新

  • drbvm26000 2015-06-03 19:11
    关注

    Unless you do not have any specific reason to do so, there is no need to convert the XML into an array. If your goal is just to grab the values mentioned, you could use an XPath query to get them directly from the XML:

    $url            = 'http://auroserver.com/sandbox/xml.xml';
    $fileContents   = trim(file_get_contents($url));
    
    $xml = new SimpleXMLElement($fileContents);
    $table = $xml->xpath('//NewDataSet/Table')[0]; # 0 = first item
    echo "CardNo:      " . $table->CardNo, PHP_EOL;
    echo "Status:      " . $table->Status, PHP_EOL;
    echo "Ebalance:    " . $table->Ebalance, PHP_EOL;
    echo "RewardPoint: " . $table->RewardPoint, PHP_EOL;
    

    Output:

    CardNo:      6048180000002031
    Status:      true
    Ebalance:    92900
    RewardPoint: 6900
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?