2014-04-10 98 views
0

我從使用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/>"; 
} 
?> 
+0

您可能需要拼寫在調用'simplexml_load_string'時輸出命名空間。請參閱[這些文檔](http://www.php.net/manual/en/function.simplexml-load-string.php),尤其是'ns'和'is_prefix'參數。 – collapsar

+0

@collapsar - 試過但無法做到。 http://viper-7.com/aOjiAA – Srihari

回答

0

注:把你的代碼放到你的問題,你會更容易讓別人幫你。

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

你需要嵌套循環迭代。首先,您需要正確選擇<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

+0

自開始以來,我已經在代碼中提出了這個問題。只是xml非常龐大,代碼仍然隱藏在底部。 感謝您的幫助,並解決了問題 – Srihari

+0

@Srihari哦對不起,我沒有向下滾動。 – michi