dreinuqm992401 2016-06-29 09:14
浏览 93
已采纳

PHP DOMDocument:如何使用COLONS解析自定义XML / 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>

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

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

  1. How to parse (read) the nodes like xCal:description
  2. Is it because of the similar node names, like: description and the xCal:description ?

(I can't change the RSS source since it's not under my control.)

Please kindly help.

  • 写回答

2条回答 默认 最新

  • douju1953 2016-06-29 09:29
    关注

    Use getElementsByTagNameNS():

    $node->getElementsByTagNameNS("urn:ietf:params:xml:ns:xcal", "description")->item(0)->nodeValue
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)