Load just XPath search to XMLReader memory?

前端 未结 1 981
暖寄归人
暖寄归人 2021-01-29 09:13

Can i somehow do this? XMLReader is pull parser, so i expect from him to give me just data i search, but it loads whole document into memory and then gives me search from his me

相关标签:
1条回答
  • 2021-01-29 10:05

    You are still searching the entire OddsList. Try using the XMLReader something like this:

    $reader = new XMLReader();
    $reader->open($url);
    $dom = new DomDocument();
    
    // Find first occurrence of Odds
    while ($reader->read() && $reader->name !== 'Odds');
    
    // Iterate Odds
    while ($reader->name === 'Odds'){
        $odds = simplexml_import_dom($dom->importNode($reader->expand(), true));
    
        // Process those having the correct Bookmaker child value
        if ($odds->xpath('./Bookmaker[string()="Interwetten"]')) {
            var_dump($odds);
        }
    
        // Move to the next one
        $reader->next('Odds');
    }
    

    Output:

    object(SimpleXMLElement)#4 (6) {
      ["FixtureMatch_Id"]=>
      string(6) "346076"
      ["Bookmaker"]=>
      string(11) "Interwetten"
      ["UpdatedDate"]=>
      string(23) "2015-06-20T19:42:33.113"
      ["Type"]=>
      string(14) "Over/Under 2.5"
      ["HomeOdds"]=>
      string(4) "1.85"
      ["AwayOdds"]=>
      string(4) "1.75"
    }
    object(SimpleXMLElement)#4 (7) {
      ["FixtureMatch_Id"]=>
      string(6) "346076"
      ["Bookmaker"]=>
      string(11) "Interwetten"
      ["UpdatedDate"]=>
      string(23) "2015-06-20T19:42:31.397"
      ["Type"]=>
      string(3) "1X2"
      ["HomeOdds"]=>
      string(3) "1.8"
      ["DrawOdds"]=>
      string(3) "3.4"
      ["AwayOdds"]=>
      string(1) "4"
    }
    
    0 讨论(0)
提交回复
热议问题