Search code examples
phpxmldomxpath

query XML file and loop through only nodes that have specific element names and attribute values


I have one xml file, which is 16.4MB in size. I need to select and print only the programme nodes that have a channel attribute value of:

BHT 1

here is xml file url: http://epg.com/epg.xml


Solution

  • Would something like this work?

     $epgdoc = new DOMDocument();
     // put the acutal path to your document here
     $epgdoc->load('epg.xml');
    
     $xpathvar = new Domxpath($epgdoc);
    
     $queryResult = $xpathvar->query("//channel[@id='BHT 1']");
     foreach($queryResult as $result){
             echo $result->textContent;
     }
    

    I think if you play around with that you can get what you need.