PHP的DOM文档获取节点值,其中属性值 [英] php domdocument get node value where attribute value is
本文介绍了PHP的DOM文档获取节点值,其中属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我说XML是这样的:
<record>
<row name="title">this item</row>
<row name="url">this url</row>
</record>
现在我在做这样的事情:
Now I'm doing something like this:
$xml = new DOMDocument();
$xml->load('xmlfile.xml');
echo $xml->getElementByTagName('row')->item(0)->attributes->getNamedItem('title')->nodeValue;
但是,这只是给了我:
But this just gives me:
注意:试图让非对象ID属性
NOTICE: Trying to get property of non-object id
是否有人知道如何让这里的name属性的值为称号的节点值?
Does anybody know how to get the node value where the "name" attribute has value "title"?
推荐答案
尝试:
$xml = new DomDocument;
$xml->loadXml('
<record>
<row name="title">this item</row>
<row name="url">this url</row>
</record>
');
$xpath = new DomXpath($xml);
// traverse all results
foreach ($xpath->query('//row[@name="title"]') as $rowNode) {
echo $rowNode->nodeValue; // will be 'this item'
}
// Or access the first result directly
$rowNode = $xpath->query('//row[@name="title"][1]')->item(0);
if ($rowNode instanceof DomElement) {
echo $rowNode->nodeValue;
}
这篇关于PHP的DOM文档获取节点值,其中属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文