SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    698
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question XMLReader Search

    Hi guys,

    Is there anyway using XMLReader (or SimpleXML for that matter) where I can search for all nodes with a particular name, rather than having to process through them each? At the moment I'm having to do:

    Code PHP:
    $xml = "URI/to/XML/file";
    $reader = new XMLReader;
    $reader->open($xml);
    $reader->open($xml);
    while ($reader->read()) {
       if ($reader->nodeType == XMLREADER::ELEMENT && $reader->localName == 'ResultCode') {
          print "Found it!";
          break;
       }
    }
    $reader->close();
    Is there a better way to do this?

    Thanks guys

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sure, XPath is your friend here.
    PHP Code:
    <?php
    $xml 
    = new SimpleXMLElement($filenulltrue);

    foreach(
    $xml->xpath('//ResultCode') as $item){
        
    #use item
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    698
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Excellent. Just tried this:

    Code php:
    <?php
                // XMLReader Example
                $stime  = microtime(TRUE);
                $reader = new XMLReader;
                $reader->xml($xml);
                while ($reader->read()) {
                   if ($reader->nodeType == XMLREADER::ELEMENT && $reader->localName == $node) {
                      //echo $reader->readString().'<br>';
                      break;
                   }
                }
                $reader->close();
                $etime  = microtime(TRUE) - $stime;
                echo "XMLReader: $etime<br>";
                unset($reader);
     
                // SimpleXMLElement Example
                $stime  = microtime(TRUE);
                $reader = new SimpleXMLElement($xml);
                foreach($reader->xpath('//ResultCode') as $item){
                    //echo "$item<br>";
                }
                $etime  = microtime(TRUE) - $stime;
                echo "SimpleXMLElement: $etime<br><br><br>";
    ?>

    The output from these examples are consistently around this (one page example):

    XMLReader: 0.00028204917907715
    SimpleXMLElement: 0.00014901161193848


    XMLReader: 0.00050997734069824
    SimpleXMLElement: 0.00013589859008789

    So, my solution is anywhere between twice as slow and five times as slow (approx) to yours. Thanks

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Or even;

    PHP Code:
    $xml = new DOMDocument;
    $xml->load($file);

    foreach(
    $xml->getElementsByTagName('ResultCode') as $item){
        
    // use item

    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    698
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Of course. Was just looking at DOMDocument last week. I'll see how it performs in relation, though I'm quite pleased with the performance of the SimpleXMLElement version


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •