关于php:Simple XML-处理节点中的冒号

Simple XML - Dealing With Colons In Nodes

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

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

我正在尝试获取缩略图。


解决方案在这篇不错的文章中进行了解释。 您需要children()方法来访问包含名称空间的XML元素。 此代码段摘自该文章:

1
2
3
4
5
$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
$item->{'itunes:duration'}


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

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

您可以提供带有xml声明的代码段吗?


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

为了从以下来源获取>的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<item>
  My important article
  <pubDate>Mon, 29 Feb 2017 00:00:00 +0000</pubDate>
  <link>https://myxmlsource.com/32984</link>
  <guid>https://myxmlsource.com/32984</guid>
  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>

使用以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
$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);
}