views:

343

answers:

3

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;
?>
A: 

Try

$reader->name

and

$reader->value

They supposed to be "The qualified name of the node" and "The text value of the node" according to this page http://www.php-editors.com/php_manual/ref.xmlreader.html

Apparently that's how people are using it: http://www.google.com/codesearch/p?hl=pl#_rn0kgFhkQA/redir/docvert/60463/url_tgz/docvert-3.2.3.tar.gz%7CP-uLGAoGHyM/docvert/core/process/ParseOpenDocument.php&amp;q=xmlreader%20file:%5C.php$

Maybe more examples of use can be found here: http://www.google.com/codesearch?q=xmlreader+file:.php$

Kamil Szot
Would these values not be based on XMLReader? I'm using DOM to get my filtered results.Tried $reader->name... anyway and nothing was returned.
rossjha
This supposed to be properties of XMLReader class. I think you should see something under $reader->name Try it on some minimal example.
Kamil Szot
Hi thanks for your help. I think i've got it. My query was the problem. Once i got to grips with xpath i was able to return nodeName and nodeValue for each of the tags.
rossjha
Could you upvote and/or accept my answer, because it answers your question?
Kamil Szot
A: 

you may also try this nice class: http://simplehtmldom.sourceforge.net/ I am using this for getting stuff out of html but it may work for xml documents too.

qxxx
Thanks very much, i think i've figured it out, but thanks for the link.
rossjha
A: 

Still a bit rough, but this is what i'm after. I figured out that my xpath query was causing the problem.

<?php
$reader = new XMLReader();
$reader->open('include/staff.xml');
$keywords = '';
$query = "//member[groups='Research'][contains(translate(forename,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') ,'$keywords') or contains(translate(surname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'), '$keywords')]/*";
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);
                $results = $xp->query($query);
            }
    }
}
$member = array();
$staff = array();
echo $results->length;
for($i=1; $i<$results->length; $i++){
    if($results->item($i)->nodeName !== 'id'){
        $member[$results->item($i)->nodeName] = $results->item($i)->nodeValue;
    }else{
        array_push($staff, $member);
    }
}
array_push($staff, $member);
var_dump($staff);

?>

rossjha