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

倖福魔咒の 提交于 2019-12-04 20:15:38

You XML is invalid, the 'x-trumba' prefix is not defined, and the closing tags of the elements use the 'xCal' prefix, refering to urn:ietf:params:xml:ns:xcal.

So replacing the prefix of the opening tags with 'xCal' and fixing the closing tags for 'author' makes the XML valid.

Then it is possible to register the xCalendar namespace and use Xpath to fetch the custom field contents:

$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";
}

Output:

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

The Xpath expression use a condition ([@name="description"]) to filter the customfield element nodes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!