dongzhu3548 2014-04-10 10:31 采纳率: 0%
浏览 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

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

报告相同问题?

悬赏问题

  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 就很莫名其妙,本来正常的Excel,突然变成了这种一格一页
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?