【发布时间】:2016-02-25 11:13:04
【问题描述】:
试图从<af:location>value</af:location> 中取出值。首先;是的,我搜索了 很多 来寻找如何做的答案。在 Stackoverflow 上阅读了很多问题并尝试了很多代码。但就是不能让它工作。
我无法显示我所做的所有尝试,因为我不记得我尝试过的所有事情。
这是我正在使用的代码的精简版本:
$xml_dump = Feed::curl_file_get_contents($url);
libxml_use_internal_errors(true);
if ($xml = simplexml_load_string($xml_dump))
{
}
例如我已经尝试过:
- 命名空间参数
'af'in ->simplexml_load_string($xml_dump, null, 0, 'af', true) -
$xml->getNamespaces(true) $sxe = new SimpleXMLElement($xml_dump); $namespaces = $sxe->getNamespaces(true);
但它们都不起作用。
$xml_dump 包含以下内容:
<?xml version="1.0"?>
<rss version="2.0" xmlns:af="http://www.example.se/rss">
<channel>
<title>RSS Title - Example.se</title>
<link>http://www.example.se</link>
<description>A description of the site</description>
<pubDate>Wed, 24 Feb 2016 12:20:03 +0100</pubDate>
<item>
<title>The title</title>
<link>http://www.example.se/2.1799db44et3a9800024.html?id=233068</link>
<description>A lot of text. A lot of text. A lot of text.</description>
<guid isPermaLink="false">example.se:item:233068</guid>
<pubDate>Wed, 24 Feb 2016 14:55:34 +0100</pubDate>
<af:profession>16/5311/5716</af:profession>
<af:location>1/160</af:location>
</item>
</channel>
</rss>
已解决!
答案是:
$loc = $item->xpath('af:location');
echo $loc[0];
【问题讨论】:
-
您应该始终为 Xpath 注册自己的前缀。像这样:stackoverflow.com/a/24101080/2265374
标签: php xml namespaces simplexml