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>
答案 0 :(得分:3)
首先,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/>';
}
答案 1 :(得分:1)
你的xpath错了。 33333是手机不是id。
尝试
$query = '/people/person[phone="33333"]';
另外
他们没有身份
echo 'id:'. $entries->id.'<br/>';
echo 'name:'.$entries->name.'<br/>';
尝试删除id部分
答案 2 :(得分:0)
试试这个:
<?php
$xml = new DOMDocument();
$xml->load('test.xml');
$xpath = new DOMXPath($xml);
$query = '/people/person/phone';
$entries = $xpath->query($query);
foreach($entries as $entry)
{
echo $entry;
}
?>