Simple XML - Dealing With Colons In Nodes

前端 未结 4 420
悲&欢浪女
悲&欢浪女 2020-11-22 14:40

I\'m trying to read an RSS feed from Flickr but it has some nodes which are not readable by Simple XML (media:thumbnail, flickr:profile, and so on)

相关标签:
4条回答
  • 2020-11-22 14:58

    You're dealing with a namespace? I think you need to use the ->children method.

    $ns_dc = $item->children('http://namespace.org/');
    

    Can you provide a snippet with the xml declaration?

    0 讨论(0)
  • 2020-11-22 14:59

    The solution is explained in this nice article. You need the children() method for accessing XML elements which contain a namespace. This code snippet is quoted from the article:

    $feed = simplexml_load_file('http://www.sitepoint.com/recent.rdf'); 
    foreach ($feed->item as $item) { 
        $ns_dc = $item->children('http://purl.org/dc/elements/1.1/'); 
        echo $ns_dc->date; 
    }
    0 讨论(0)
  • 2020-11-22 15:00

    With the latest version, you can now reference colon nodes with curly brackets.

    $item->{'itunes:duration'}
    
    0 讨论(0)
  • 2020-11-22 15:04

    An even simpler method using PHP of accessing namespaced XML nodes without declaring a namespace is....

    In order to get the value of <su:authorEmail> from the following source

    <item>
      <title>My important article</title>
      <pubDate>Mon, 29 Feb 2017 00:00:00 +0000</pubDate>
      <link>https://myxmlsource.com/32984</link>
      <guid>https://myxmlsource.com/32984</guid>
      <author>Blogs, Jo</author>
      <su:departments>
        <su:department>Human Affairs</su:department>
      </su:departments>
      <su:authorHash>4f329b923419b3cb2c654d615e22588c</su:authorHash>
      <su:authorEmail>hIwW14tLc+4l/oo7agmRrcjwe531u+mO/3IG3xe5jMg=</su:authorEmail>
      <dc:identifier>/32984/Download/0032984-11042.docx</dc:identifier>
      <dc:format>Journal article</dc:format>
      <dc:creator>Blogs, Jo</dc:creator>
      <slash:comments>0</slash:comments>
    </item>
    

    Use the following code:

    $rss = new DOMDocument();
    
    $rss->load('https://myxmlsource.com/rss/xml');
    
    $nodes = $rss->getElementsByTagName('item');
    
    foreach ($nodes as $node) {
        $title = $node->getElementsByTagName('title')->item(0)->nodeValue;
        $author = $node->getElementsByTagName('author')->item(0)->nodeValue;
        $authorHash = $node->getElementsByTagName('authorHash')->item(0)->nodeValue;
        $department = $node->getElementsByTagName('department')->item(0)->nodeValue;
        $email = decryptEmail($node->getElementsByTagName('authorEmail')->item(0)->nodeValue);
    }
    
    0 讨论(0)
提交回复
热议问题