duanleiming2014 2011-07-14 19:30
浏览 93
已采纳

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

I'm using PHP and CURL to get XML, but I'm having problems accessing a specific type of attributes.

Say the XML document is like this:

<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>

I want to access the url attribute within the tag, but can't seem to get around the namespace problem.

Currently, I've tried using:

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

1条回答 默认 最新

  • doufei3152 2011-07-14 19:34
    关注

    See http://www.php.net/manual/en/domdocument.getelementsbytagnamens.php. You need to register the namespace with the namespaceURI in your XML and specify it.

    ADDED

    In the beginning I didn't see you've got xmlns:media attribute in your XML. So all you need to change is line

    $promos = $item->getElementsByTagNameNS("http://mediaurl.com/", "content");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法