我從使用Guzzle的服務器獲取SimpleXML對象。打印爲字符串的響應看起來像有效的XML。但是當我嘗試獲取單個元素值時,我變得空白。如何打印SimpleXmlElement對象的值
請幫我看看是什麼問題。
我的代碼是在這裏以及在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/>";
}
?>
您可能需要拼寫在調用'simplexml_load_string'時輸出命名空間。請參閱[這些文檔](http://www.php.net/manual/en/function.simplexml-load-string.php),尤其是'ns'和'is_prefix'參數。 – collapsar
@collapsar - 試過但無法做到。 http://viper-7.com/aOjiAA – Srihari