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 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸