PHP DOMDocument:如何解析带有CUSTOM字段名的xml/rss标签


PHP DOMDocument : How to parse xml/rss Tags with CUSTOM field names?

我有以下RSS要解析,类似于:

<?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>
            <x-trumba:customfield name="description">This is the description about apples</xCal:customfield>
            <x-trumba:customfield name="category">Fruits,Food,Apple</xCal:customfield>
        </item>
        <item>
            <title>About Oranges</title>
            <author>Marry L. Jones</title>
            <x-trumba:customfield name="description">This is the description about oranges</xCal:customfield>
            <x-trumba:customfield name="category">Fruits,Food,Orange</xCal:customfield>
        </item>
    </channel>
</rss>
在PHP中,我只知道如何读取前两个节点,例如:
$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,
}

但是,这些都是问题:

<x-trumba:customfield name="description">This is the description about apples</xCal:customfield>
<x-trumba:customfield name="category">Fruits,Food,Apple</xCal:customfield>

请帮助:

  • 如何解析 <x-trumba:customfield name="description">这样的最后节点?

(我不能更改RSS源,因为它不在我的控制之下。)

您的XML无效,没有定义'x-trumba'前缀,并且元素的结束标记使用'xCal'前缀,指的是urn:ietf:params:xml:ns:xcal

因此,用'xCal'替换开始标签的前缀,并为'author'修复结束标签,使XML有效。

然后可以注册xCalendar名称空间并使用Xpath获取自定义字段内容:

$rss = new DOMDocument();
$rss->load( "http://www.example.com/books.rss" );
$xpath = new DOMXpath($rss);
$xpath->registerNamespace('x', 'urn:ietf:params:xml:ns:xcal');
foreach( $xpath->evaluate("//item") as $item ) {
    echo $xpath->evaluate('string(title)', $item), "'n";
    echo $xpath->evaluate('string(x:customfield[@name="description"])', $item), "'n";
}
输出:

About Apples
This is the description about apples
About Oranges
This is the description about oranges

Xpath表达式使用条件([@name="description"])来过滤customfield元素节点。