php domdocument get node value where attribute value is

醉酒当歌 提交于 2019-11-28 07:39:44
Yoshi

Try:

$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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!