在命名空间标记中解析XML属性(PHP)

时间:2011-07-14 19:30:05

标签: php xml namespaces

我正在使用PHP和CURL来获取XML,但是我在访问特定类型的属性时遇到了问题。

说XML文档是这样的:

<item>
    <title>Example Title</title>
    <link>http://www.google.com</link>
    <description>Sample desc here.</description>
    <media:content xmlns:media="http://mediaurl.com/" url="http://www.exampleurl.com/"
    type="image/jpeg" medium="image" height="226" width="571">
    </media:content>
</item>

我想访问标记中的url属性,但似乎无法解决命名空间问题。

目前,我尝试过使用:

$promos = $item->getElementByTagNameNS("media", "content");
foreach ($promos as $promo)
{
    $promoImage = $promo->getAttribute("url");
    break;
}
echo $promoImage;

1 个答案:

答案 0 :(得分:1)

http://www.php.net/manual/en/domdocument.getelementsbytagnamens.php。您需要在XML中使用namespaceURI注册命名空间并指定它。

<强> ADDED

一开始我没有看到你的XML中有xmlns:media属性。所以你需要改变的是行

$promos = $item->getElementsByTagNameNS("http://mediaurl.com/", "content");