Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 简单XML—处理节点中的冒号_Php_Xml_Simplexml - Fatal编程技术网

Php 简单XML—处理节点中的冒号

Php 简单XML—处理节点中的冒号,php,xml,simplexml,Php,Xml,Simplexml,我正在尝试从Flickr读取RSS提要,但它有一些简单XML无法读取的节点(媒体:缩略图,Flickr:profile,等等) 我怎样才能避开这个?当我看DOM的文档时,我的头很痛。所以我想避免它,因为我不想学习 顺便说一下,我正在尝试获取缩略图。您正在处理名称空间?我认为您需要使用->children方法 $ns_dc = $item->children('http://namespace.org/'); 您能提供一个包含xml声明的代码片段吗?中介绍了解决方案。您需要children

我正在尝试从Flickr读取RSS提要,但它有一些简单XML无法读取的节点(
媒体:缩略图
Flickr:profile
,等等)

我怎样才能避开这个?当我看DOM的文档时,我的头很痛。所以我想避免它,因为我不想学习


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

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

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

您能提供一个包含xml声明的代码片段吗?

中介绍了解决方案。您需要
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; 
}

在最新版本中,现在可以使用花括号引用冒号节点

$item->{'itunes:duration'}

使用PHP访问带名称空间的XML节点而不声明名称空间的一种更简单的方法是

为了从以下来源获取
的值

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

相关:如果XML有这个标签,那么你将如何获得链接?@PapaDeBeau我建议作为一个单独的问题来问这个问题。这是唯一有帮助的答案。非常感谢。
$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);
}