简单的XML - 处理节点中的冒号

时间:2009-07-27 01:48:52

标签: php xml

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

我如何绕过这个?当我查看DOM的文档时,我的头疼。所以我想避免它,因为我不想学习。

顺便说一下,我正试着搞缩略图。

4 个答案:

答案 0 :(得分:18)

解决方案在this nice article中解释。您需要children()方法来访问包含命名空间的XML元素。此代码段引自文章:

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

答案 1 :(得分:3)

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

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

您能提供带有xml声明的片段吗?

答案 2 :(得分:2)

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

$item->{'itunes:duration'}

答案 3 :(得分:0)

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

为了获得<su:authorEmail&gt;的值来自以下来源

<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);
}
相关问题