我试图加载使用XML文件:
$xml = simplexml_load_file('https://xxxxx/xxxxx.xml');
和XML文件中有一些评论,你可以在下面的PIC,我想在创建我的表使用的数据在评论中看到其中:
但是,当我想我的var_dump见[“意见”]作为数据:
["comment"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#5 (0) {
}
[1]=>
object(SimpleXMLElement)#7 (0) {
}
}
问题是,我怎么能检索的意见,所以我可以使用他们给出的郎名称的内容?
使用的file_get_contents来代替:
$xml = file_get_contents('https://xxxxx/xxxxx.xml');
如果你需要XML加载到目标那么我认为你必须使用DOM文档():
$dom = new DOMDocument();
$dom->load('https://xxxxx/xxxxx.xml');
$xpath = new DOMXpath($dom);
$comment = $xpath->evaluate('string(//channel/item[1]/comment())');
echo $comment;
后续代码var_dump($ XML); //显示评论
您将需要使用DOM文档作为SimpleXML的不好征求意见。您可以使用XPath找到你的意见后(使用normalize-space()
删除文本周围的空间)和following-sibling
返回以下节点。最后一行只输出它的发现的元素,你将需要处理它的要求(使用[0]
作为query
将返回节点列表只想和你是第一个)。
$doc = new DOMDocument();
$doc->load($file);
$xp = new DOMXPath($doc);
$lang = "Finland";
$langPop = $xp->query("//comment()[normalize-space(.)='$lang']/following-sibling::*[1]");
echo $doc->saveXML($langPop[0]);
随着一些示例数据...
<base>
<!-- Finland -->
<languagePopulation>Finland data
</languagePopulation>
<!-- England -->
<languagePopulation>England data
</languagePopulation>
</base>
会给
<languagePopulation>Finland data
</languagePopulation>