2013-02-21 99 views
0

我解析XML與PHP simplexml_load_file功能。 XML是;XML解析第二個標籤

<item> 
<Title>TEST</Title> 
<Image primary="true"/> 
<Image> 
    http://www.domain.com/image.jpg 
</Image> 
</item> 

我真的在解析標題標籤;

$var->item->Title 

但解析圖像標籤給出了錯誤的結果;

$var->item->Image 

我該如何取; http://www.domain.com/image.jpg結果?

回答

1

你的XML格式化improperly--如果你有多個圖像,並要設置一個伯,你會使用

<item> 
<Title>TEST</Title> 
<Images> 
    <Image primary="true">http://www.domain.com/image1.jpg</Image> 
    <Image>http://www.domain.com/image2.jpg</Image> 
</Images> 
</item> 

然後遍歷在PHP中Images陣列訪問的路徑圖片。

或者如果你知道你只使用一個單一的形象,設置Primary屬性相同的圖像節點上:

<item> 
<Title>TEST</Title> 
<Image primary="true">http://www.domain.com/image1.jpg</Image> 
</item> 
+0

感謝您的回答。這個xml是外部文件。所以我不能修改這個。 – pheaselegen 2013-02-21 19:06:30

+1

然後,你將不得不訪問'Image'爲陣列,並選擇第二個:'$ VAR->用品 - >圖像[1]' – user101289 2013-02-21 19:11:10

+0

確定感謝。這解決了我的問題。 – pheaselegen 2013-02-21 19:13:21

1

你可以得到「圖像」節點的屬性,這樣

var_dump($var->Title); 
var_dump($var->Image->attributes()->primary); 
+0

感謝您的回答。但我不想要圖像的屬性。我想要圖片網址。 – pheaselegen 2013-02-21 19:11:35

+0

@pheaselegen對不起。顯然我不明白你的問題。 – Winston 2013-02-21 19:17:08