2012-08-13 102 views
0

我想從RSS源中使用簡單XML讀出特定屬性。項目的使用簡單XML讀取XML屬性

例子:

<item> 
    <category>3500</category> 
    <category>20</category> 
    <category>17</category> 
    <title>Blabla</title> 
    <description>Blabla</description> 
    <content:encoded>Blabla</content:encoded> 
    <link>http://...</link> 
    <guid isPermaLink="false">http://...</guid> 
    <media:content url="http://image.jpg" medium="image"> 
       <media:title type="html">caption needed</media:title> 
    </media:content> 
    <pubDate>Mon, 13 Aug 2012 11:40:53 +0200</pubDate> 
</item> 

我想讀的URL這是在媒體:內容。

讀書,我使用的其他標籤:$ XML->通道 - >項目[$ i] - >標題

讀取介質url屬性我想:$ XML->通道 - >項目[$ i] - > media-> attributes() - > url

但是,這只是返回NULL。它與以下內容有關:內容是一個命名空間。

回答

1

元素的名稱實際上是content,而不是media(它只是一個任意的前綴,應該與命名空間相關聯以形成格式良好的XML)。與encoded是元素名稱而不是content相同。

$xml->item[0]->children('whatever-the-namespace-tied-to-media-is')->content->attributes()->url 
+0

感謝Brett的評論。 – zaboe 2012-08-13 13:24:25

+0

我其實剛剛找到解決方案。 '$ namespace = $ xml-> channel-> item [$ i] - > children(「http://search.yahoo.com/mrss/」); $ img = $ namespace - > {「content」} - > attributes() - > url;' 我對兒童和屬性有點困惑。 也許這個解決方案對其他人也有用。 – zaboe 2012-08-13 13:27:45