【问题标题】:Simple XML - Dealing With Colons In Nodes简单 XML - 处理节点中的冒号
【发布时间】:2010-11-14 05:45:54
【问题描述】:

我正在尝试从 Flickr 读取 RSS 提要,但它有一些简单 XML 无法读取的节点(media:thumbnailflickr:profile 等)。

我该如何解决这个问题?当我查看 DOM 的文档时,我的头很痛。所以我想避免它,因为我不想学习。

顺便说一下,我正在尝试获取缩略图。

【问题讨论】:

标签: php xml simplexml


【解决方案1】:

解决方案在this nice article 中进行了说明。您需要 children() 方法来访问包含命名空间的 XML 元素。这段代码sn-p引用自文章:

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

【讨论】:

  • 如果 XML 有这个标签 那么你将如何获得链接?
  • @PapaDeBeau 我建议将此作为一个单独的问题提出。
【解决方案2】:

使用最新版本,您现在可以使用大括号引用冒号节点。

$item->{'itunes:duration'}

【讨论】:

  • 唯一真正有帮助的答案。谢谢。
【解决方案3】:

您正在处理命名空间?我认为您需要使用 ->children 方法。

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

你能提供一个带有 xml 声明的 sn-p 吗?

【讨论】:

    【解决方案4】:

    使用 PHP 访问命名空间 XML 节点的更简单的方法无需声明命名空间是......

    为了从以下来源获取<su:authorEmail>的值

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

    使用以下代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-26
      相关资源
      最近更新 更多