Get tag name and value of a given node using XMLReader, DOM, Xpath
Posted
by rossjha
on Stack Overflow
See other posts from Stack Overflow
or by rossjha
Published on 2010-03-04T09:52:47Z
Indexed on
2010/06/08
17:02 UTC
Read the original article
Hit count: 328
I need to query an xml document and then display specific tag values, e.g. forename, surname, group(dept), job_title.
I'm using XMLReader as i may need to work with large XML files. I using DomXPath to filter the data, but i don't know how to retrieve the nodeName and value for each element. The code below only returns 'member' as the node name?
Any help would be appreciated.
<?php
$reader = new XMLReader();
$reader->open('include/staff.xml');
while ($reader->read()){
switch($reader->nodeType){
case(XMLREADER::ELEMENT):
if($reader->localName === 'staff'){
$node = $reader->expand();
$dom = new DomDocument();
$dom->formatOutput = true;
$n = $dom->importNode($node, true);
$dom->appendChild($n);
$xp = new DomXpath($dom);
$res = $xp->query("/staff/member[groups='HR']");
}
}
}
echo $res->item(0)->nodeName;
echo $res->item(0)->nodeValue;
?>
© Stack Overflow or respective owner