dongzhu3548 2014-04-10 10:31
浏览 106
已采纳

如何打印SimpleXmlElement对象的值

I am getting a SimpleXML object back from a server using Guzzle. The response printed as string looks like valid XML. But when I try to get the individual element values, I am getting blank.

Please help me find what the problem is.

My code is here as well as on Viper-7:

<?php
    $xml = '<?xml version="1.0" encoding="UTF-8"?>
<RespOrderHistory xmlns="http://www.example.com/testService/xsd/OrderHistoryResponse_v1.xsd" xmlns:typ="http://www.example.com/testService/xsd/Types_v1.xsd">
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Cancelled</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Cancelled</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Cancelled</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Cancelled</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Cancelled</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>138</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-07T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-07T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>133</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-07T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-07T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>128</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-07T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-07T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
</RespOrderHistory>';

$sXml = simplexml_load_string ($xml);

foreach($sXml as $order){
    echo $order->getName()." ";
    print_r($order);
    echo "<br/>";
}
?>
  • 写回答

1条回答 默认 最新

  • dpjj4763 2014-04-10 21:53
    关注

    Note: By putting your code into your question, you will make it easier for others to help you.

    $xml = simplexml_load_string($x); // assume XML in $x
    

    You will need 2 nested loops to iterate. First, you will want to select the <OrdersList>-nodes correctly, see the outer loop.

    foreach ($xml->OrdersList as $orders) {
    

    The children of <OrdersList> are namespaced:
    $order->OrderNumber doesn't work, but the children()-method does the job:

        foreach ($orders->children("typ", TRUE) as $name => $value) {
    
            echo "$name: $value <br />";
    
        }
    
    }
    

    see it working: http://viper-7.com/bS2pEJ

    Recommended reading: http://www.php.net/manual/en/simplexmlelement.children.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常