假设我的XML是这样的:
<record>
<row name="title">this item</row>
<row name="url">this url</row>
</record>
现在我要这样做:
$xml = new DOMDocument();
$xml->load('xmlfile.xml');
echo $xml->getElementByTagName('row')->item(0)->attributes->getNamedItem('title')->nodeValue;
但是这给了我:
注意:试图获取非对象id
的属性有没有人知道如何获得节点值,其中"name"属性值"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;
}
foreach ($xml->getElementsByTagName('row') as $element)
{
if ($element->getAttribute('name') == "title")
{
echo $element->nodeValue;
}
}
$xpath = new DOMXPath( $xml );
$val = $xpath->query( '//row[@name="title"]' )->item(0)->nodeValue;