2018-01-25 00:46
浏览 96

使用PHP DOMDocument从带有冒号的命名空间的xml节点获取值

I'm parsing xml document using DOMDocument class. First I've selected all nodes with name 'item' using $xml->getElementsByTagName('item') method. My sample xml file looks like this:


However my problem is getting value from nested tag with namespace name with colon sign. I get values from nodes without namespaces in foreach using $node->getElementsByTagName('description')->item(0)->nodeValue and there is no errors.

I also found method getElementsByTagNameNs() to get nodes with it's namespaces. However when i try to get nodeValue like previous nodes without namespace I get "PHP Notice: Trying to get property of non-object". I think it is strange because getElementsByTagNameNs()->item(0) returns object DOMElement.

Do you know how to take value from node with namespace, in this case <ns:author></dc:creator> element?

图片转代码服务由CSDN问答提供 功能建议

我正在使用DOMDocument类解析xml文档。 首先,我使用 $ xml-&gt; getElementsByTagName('item')方法选择名为'item'的所有节点。 我的示例xml文件如下所示:

 &lt; item&gt; 
&lt; title&gt; ...&lt; / link&gt; 
&lt; description&gt; ...&lt;  ; / description&gt; 
&lt; pubDate&gt; ...&lt; / pubDate&gt; 
&lt; ns:author&gt; ...&lt; / dc:creator&gt; 
&lt; / item&gt; 

但是我的问题是从带有冒号的命名空间名称的嵌套标记中获取值。 我使用 $ node-&gt; getElementsByTagName('description') - &gt; item(0) - &gt; nodeValue foreach 中获取没有名称空间的节点的值,并且没有 错误。

我还发现方法getElementsByTagNameNs()来获取带有命名空间的节点。 然而,当我尝试像没有命名空间的先前节点那样获得nodeValue时,我得到“PHP注意:尝试获取非对象的属性”。 我认为这很奇怪,因为 getElementsByTagNameNs() - &gt; item(0)返回对象DOMElement。

您是否知道如何从带有命名空间的节点获取值, 在这种情况下&lt; ns:author&gt;&lt; / dc:creator&gt; 元素?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doubiao7410 2018-01-25 11:48

    As the document fragment you give isn't complete and even valid, I've had to make a few changes. You say you want the value of <ns:author>...</dc:creator> which is not a valid element as the open and close names are different in both name and namespace, you would expect something like <ns:author>...</ns:author>

    As an example of how you can fetch the values from a namespace (using some corrected and some guessed alterations to the XML)...

    $xml = <<<XML
    <?xml version="1.0"?>
    <items xmlns:ns="http://some.url">
    $dom = new DOMDocument( '1.0', 'utf-8' );
    $items = $dom->getElementsByTagName('item');
    foreach ( $items as $item )  {
        $author = $item->getElementsByTagNameNS("http://some.url", "*")->item(0)->nodeValue;
        echo "Author = ".$author.PHP_EOL;

    In getElementsByTagNameNS() you will need to put in the correct URL for this namespace, which should be in the source document.

    This outputs...

    Author = author1
    解决 无用
    打赏 举报

相关推荐 更多相似问题