PHP DOMDocument:如何使用COLONS解析自定义XML/RSS标记名称? [英] PHP DOMDocument : How to parse custom XML/RSS tag names with COLONS?

查看:61
本文介绍了PHP DOMDocument:如何使用COLONS解析自定义XML/RSS标记名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解析以下RSS,如下所示:

I have the below RSS to parse, something like:

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:x-wr="http://www.w3.org/2002/12/cal/prod/Apple_Comp_628d9d8459c556fa#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:x-example="http://www.example.com/rss/x-example" xmlns:x-microsoft="http://schemas.microsoft.com/x-microsoft" xmlns:xCal="urn:ietf:params:xml:ns:xcal" version="2.0">
    <channel>
        <item>
            <title>About Apples</title>
            <author>David K. Lowie</title>
            <description>Some description about apples</description>
            <xCal:description>This is the full description about apples</xCal:description>
        </item>
        <item>
            <title>About Oranges</title>
            <author>Marry L. Jones</title>
            <description>Some description about oranges</description>
            <xCal:description>This is the full description about oranges</xCal:description>
        </item>
    </channel>
</rss>

在PHP中,我将其解析为:

In PHP, i parse it something like:

$rss = new DOMDocument();
$rss->load( "http://www.example.com/books.rss" );

foreach( $rss->getElementsByTagName("item") as $node ) {
    echo $node->getElementsByTagName("title")->item(0)->nodeValue,
    echo $node->getElementsByTagName("author")->item(0)->nodeValue,
    echo $node->getElementsByTagName("description")->item(0)->nodeValue,
    echo $node->getElementsByTagName("xCal:description")->item(0)->nodeValue,
}

我可以读取 xCal:description 节点之外的所有内容.(节点名称完全像这样: description xCal:description .)

I can read everything except the xCal:description node there. (The node names are exactly like that: description and the xCal:description.)

  1. 如何解析(读取)像这样的节点 xCal:description
  2. 是因为类似的节点名称,例如: description xCal:description 吗?

(由于不受我的控制,我无法更改RSS源.)

请帮助.

推荐答案

使用 getElementsByTagNameNS():

$node->getElementsByTagNameNS("urn:ietf:params:xml:ns:xcal", "description")->item(0)->nodeValue

这篇关于PHP DOMDocument:如何使用COLONS解析自定义XML/RSS标记名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆