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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料