[PHP] To display some attribute of the node in the XML document.

代码
<?php
$xml = <<<XML
<xml>
<body bgcolor="red">red</body>
<body bgcolor="green">green</body>
</xml>
XML;

$dom = new DomDocument();
$dom->loadXML($xml);

$xpath = new DomXpath($dom);
$nodes = $xpath->query("*[local-name()='body']", $dom->documentElement);

echo $nodes->item(0)->getAttributeNode('bgcolor')->value;
?>

The result:

red

原文地址:https://www.cnblogs.com/davidhhuan/p/1733840.html