我正在使用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;
答案 0 :(得分:1)
见http://www.php.net/manual/en/domdocument.getelementsbytagnamens.php。您需要在XML中使用namespaceURI注册命名空间并指定它。
<强> ADDED 强>
一开始我没有看到你的XML中有xmlns:media属性。所以你需要改变的是行
$promos = $item->getElementsByTagNameNS("http://mediaurl.com/", "content");