2012-07-10 72 views
0

我想從xml字符串中獲取信息並查看其中的子節點。我使用:simplexml_load_string和子節點

'Title' => $current->AttributeSets->children('ns2')->ItemAttributes->Title, 

在數組,其中$當前

$current = $parsed_xml->ListMatchingProductsResponse->ListMatchingProductsResult->Products->Product; 

命名空間NS2和孩子們ItemAttributes中和標題。當我運行這個時,我不知道標題應該在哪裏。下面是我運行腳本時的響應:

SimpleXMLElement Object() 

任何人都可以指向正確的方向嗎?我看了其他的帖子,試圖用他們的例子,但他們不適合我。 這裏是XML:

<?xml version="1.0"?> 
<ListMatchingProductsResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"> 
<ListMatchingProductsResult> 
<Products xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd"> 
<Product> 
<AttributeSets> 
<ns2:ItemAttributes xml:lang="en-US"> 
<ns2:Title>JavaScript: The Missing Manual</ns2:Title> 
+0

您需要發佈您的XML。 – nickb 2012-07-10 17:34:30

回答

1

萬一別人需要做同樣的事情,讓孩子們信息的正確方法是:不需要

'Title' => $current->AttributeSets->children('ns2', true)->ItemAttributes->Title, 

字符串,但真正是。

0

一般需要投的SimpleXML元素爲字符串來獲取值了。嘗試 'Title' => (string)$current->AttributeSets->children('ns2')->ItemAttributes->Title

+0

我嘗試添加(字符串),仍然得到相同的結果。 – Jim 2012-07-10 17:43:01

-1

另一種解決方案是在通過simplexml_load_string將其替換爲簡單的字符串替換之前刪除命名空間ns2,我認爲這有點破解,但這是我的做法。

$response = str_replace('ns2:', '', $response); 
$parse_xml = simplexml_load_string($response);