【问题标题】:PHP function for parsing xml into array that doesn't strip nodes with namespaces用于将 xml 解析为不带命名空间节点的数组的 PHP 函数
【发布时间】:2010-07-29 18:10:11
【问题描述】:

我正在尝试解析使用格式良好的 Web 评论 api 的 rss 提要,并且在提取包含名称空间的部分 XML 时遇到了一些问题。我在这里检查过:

PHP parsing XML file with and without namespaces

但似乎他的解决方案更多的是针对一个特定的命名空间。这是我的 XML:

 <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/">
<channel>
<title>Some TITLE</title>
<link>http://www.somelink.com</link>
<description>Make it happen.</description>
<dc:language />
<generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator>
<item>
<title>Blah blah blah</title>
<link>http://www.somelink.com</link>
<pubDate>Wed, 21 Jul 2010 16:30:00 GMT</pubDate>
<guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9612038</guid>
<dc:creator>Chris Pendleton</dc:creator>
<slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">

我尝试使用几个对象来排列我通常用于解析 XML 的函数,但它们忽略了所有包含命名空间的节点。建议?我真正需要的只是获取每个节点的对象。

TIA

【问题讨论】:

    标签: php xml namespaces


    【解决方案1】:

    您尝试过 XMLReader 吗? http://php.net/manual/en/book.xmlreader.php

    【讨论】:

      【解决方案2】:

      您正在解析 RSS,这是一项成熟的任务。您是否考虑过using someone else's code 而不是自己编写?有很多选择,php rss parser 的快速 Google 会找到很多好的(和一些坏的)选择...

      如果您坚持自己做,the DOM 支持命名空间——所有以“NS”结尾的方法都适用于命名空间的 XML 元素和属性。

      【讨论】:

      • 我一直在使用别人的功能。问题是我在网上找到的所有节点都不适合保留附加名称空间的节点。
      • 那是因为将命名空间节点强制转换为简单数组太难了。你真的应该使用 DOM 类进行一般的 XML 解析,或者像 Charles 建议的那样使用知名的 RSS 解析器之一。
      • 好的,我也会研究一下 DOM 类。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 2012-12-05
      相关资源
      最近更新 更多