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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容