我使用Guzzle从服务器获取SimpleXML对象。以字符串形式打印的响应看起来像有效的XML。但是当我试图获得单个元素值时,我变得空白。
请帮我找出问题所在。
我的代码就在这里以及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/>";
}
?>
答案 0 :(得分:0)
注意:通过将您的代码放入您的问题中,您可以更轻松地让其他人帮助您。
$xml = simplexml_load_string($x); // assume XML in $x
您需要 2 嵌套循环才能进行迭代。首先,您需要正确选择<OrdersList>
- 节点,请参阅外部循环。
foreach ($xml->OrdersList as $orders) {
<OrdersList>
的孩子是namespaced
:
$order->OrderNumber
不起作用,但children()
- 方法可以完成工作:
foreach ($orders->children("typ", TRUE) as $name => $value) {
echo "$name: $value <br />";
}
}
看到它有效:http://viper-7.com/bS2pEJ
推荐阅读:http://www.php.net/manual/en/simplexmlelement.children.php