2

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

2 Answers 2

2

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);

?>

Sign up to request clarification or add additional context in comments.

Comments

0

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&q=xmlreader%20file:%5C.php$

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

3 Comments

Would these values not be based on XMLReader? I'm using DOM & XPath so i think i need to use DOM methods i.e. $res->item(1)->nodeValue; to get my filtered results. Tried $reader->name... anyway and nothing was returned.
This supposed to be properties of XMLReader class. I think you should see something under $reader->name Try it on some minimal example.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.