Parse XML with PHP and XMLReader

前端 未结 2 621
野性不改
野性不改 2020-12-21 02:29

I\'ve been trying to parse a very large XML file with PHP and XMLReader, but can\'t seem to get the results I am looking for. Basically, I\'m searching a ton of information

2条回答
  •  独厮守ぢ
    2020-12-21 03:00

    Edit: Oh you want to return the parent chunk? One moment.

    Here's an example to pull out all of the postalCodes into an array.

    http://codepad.org/kHss4MdV

    
     
      Grande Gables at The Terrace
      Grande Communications
      
       635
      
      
       11111
       22222
       33333
       78746
      
      Austin
      
       
        002
       
       
        003
       
      
      
       
        Thorndale
        Milam
        TX
       
       
        Thrall
        Williamson
        TX
       
      
     ';
    
    $dom = new DOMDocument();
    $dom->loadXML($string);
    
    $xpath = new DOMXPath($dom);
    $elements= $xpath->query('//lineups/headend/postalCodes/*[text()=78746]');
    
    if (!is_null($elements)) {
      foreach ($elements as $element) {
        echo "
    [". $element->nodeName. "]"; $nodes = $element->childNodes; foreach ($nodes as $node) { echo $node->nodeValue. "\n"; } } }

    Outputs:


    [postalCode]78746

提交回复
热议问题