gpt4 book ai didi

php - xml + xpath,打印元素

转载 作者:行者123 更新时间:2023-12-03 17:07:05 25 4
gpt4 key购买 nike

Notice: Undefined property: DOMNodeList::$id in D:\wamp\www\xml\index.php on line 15
id:

Notice: Undefined property: DOMNodeList::$name in D:\wamp\www\xml\index.php on line 16
name:

<?php
$xml = new DOMDocument();

$xml->load('test.xml');

$xpath = new DOMXPath($xml);

$query = '/people/person[id="33333"]';

$entries = $xpath->query($query);

echo 'id:'. $entries->id.'<br/>';
echo 'name:'.$entries->name.'<br/>';
?>


xml文件示例:

<people>
...
<person>
<phone>33333</phone>
<name>Aadgar</name>
<last_name>Adas</last_name>
</person>
...
</people>

最佳答案

首先,id节点不存在...

$query = '/people/person[id="33333"]';


我想你要:

$query = '/people/person[phone="33333"]';


然后,您必须执行以下操作:

$entries = $xpath->query($query);

foreach ($entries as $entry) {
echo 'name:'. $entry->getElementsByTagName('name')->item(0)->nodeValue.'<br/>';
echo 'last_name:'.$entry->getElementsByTagName('last_name')->item(0)->nodeValue.'<br/>';
}


看来您正在混淆 DOMSimpleXML语法。

关于php - xml + xpath,打印元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4492674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com