2014-04-10 56 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