XPath不适用于XMLReader和SimpleXML?

时间:2015-06-21 08:13:20

标签: php xml xpath simplexml xmlreader

我从xml接收器获取源,他的XML结构如下:

<XMLSOCCER.COM>
<OddsList>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Bet-At-Home</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:32.943</UpdatedDate>
  <Type>Over/Under 2.5</Type>
  <HomeOdds>2.22</HomeOdds>
  <AwayOdds>1.58</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Interwetten</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:33.113</UpdatedDate>
  <Type>Over/Under 2.5</Type>
  <HomeOdds>1.85</HomeOdds>
  <AwayOdds>1.75</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>188Bet</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:33.27</UpdatedDate>
  <Type>Over/Under 2.5</Type>
  <HomeOdds>2.11</HomeOdds>
  <AwayOdds>1.8</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Pinnacle</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:32.803</UpdatedDate>
  <Type>Over/Under 2.5</Type>
  <HomeOdds>2.33</HomeOdds>
  <AwayOdds>1.65</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Bet365</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:34.263</UpdatedDate>
  <Type>Handicap</Type>
  <HomeOdds>3.1</HomeOdds>
  <DrawOdds>3.5</DrawOdds>
  <AwayOdds>2</AwayOdds>
  <Handicap>-1</Handicap>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Bet-At-Home</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:34.373</UpdatedDate>
  <Type>Handicap</Type>
  <HomeOdds>1.17</HomeOdds>
  <DrawOdds>5.41</DrawOdds>
  <AwayOdds>10.32</AwayOdds>
  <Handicap>1</Handicap>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Mybet</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:34.5</UpdatedDate>
  <Type>Handicap</Type>
  <HomeOdds>3.35</HomeOdds>
  <DrawOdds>3.5</DrawOdds>
  <AwayOdds>1.85</AwayOdds>
  <Handicap>-1</Handicap>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Expekt</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:33.41</UpdatedDate>
  <Type>Double Chance</Type>
  <HomeOdds>1.15</HomeOdds>
  <DrawOdds>1.85</DrawOdds>
  <AwayOdds>1.3</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Bet-At-Home</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:33.787</UpdatedDate>
  <Type>Double Chance</Type>
  <HomeOdds>1.17</HomeOdds>
  <DrawOdds>1.8</DrawOdds>
  <AwayOdds>1.25</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Mybet</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:34.037</UpdatedDate>
  <Type>Double Chance</Type>
  <HomeOdds>1.15</HomeOdds>
  <DrawOdds>1.85</DrawOdds>
  <AwayOdds>1.25</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>BetClic</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:34.16</UpdatedDate>
  <Type>Double Chance</Type>
  <HomeOdds>1.15</HomeOdds>
  <DrawOdds>1.85</DrawOdds>
  <AwayOdds>1.3</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Unibet</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:33.52</UpdatedDate>
  <Type>Double Chance</Type>
  <HomeOdds>1.22</HomeOdds>
  <DrawOdds>1.84</DrawOdds>
  <AwayOdds>1.29</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>888</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:33.647</UpdatedDate>
  <Type>Double Chance</Type>
  <HomeOdds>1.22</HomeOdds>
  <DrawOdds>1.84</DrawOdds>
  <AwayOdds>1.29</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Pinnacle</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:34.733</UpdatedDate>
  <Type>Asian Handicap</Type>
  <HomeOdds>1.92</HomeOdds>
  <AwayOdds>2</AwayOdds>
  <Handicap>-0.5</Handicap>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>WilliamHill</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:34.623</UpdatedDate>
  <Type>Asian Handicap</Type>
  <HomeOdds>1.36</HomeOdds>
  <AwayOdds>3</AwayOdds>
  <Handicap>0</Handicap>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Betfred</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:34.86</UpdatedDate>
  <Type>Asian Handicap</Type>
  <HomeOdds>1.36</HomeOdds>
  <AwayOdds>3</AwayOdds>
  <Handicap>0</Handicap>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>188Bet</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:34.967</UpdatedDate>
  <Type>Asian Handicap</Type>
  <HomeOdds>1.89</HomeOdds>
  <AwayOdds>2.03</AwayOdds>
  <Handicap>-0.5</Handicap>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Doxxbet</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:31.63</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.85</HomeOdds>
  <DrawOdds>3.3</DrawOdds>
  <AwayOdds>3.92</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>BetSafe</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:30.74</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.84</HomeOdds>
  <DrawOdds>3.25</DrawOdds>
  <AwayOdds>4.25</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Pinnacle</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:30.257</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.92</HomeOdds>
  <DrawOdds>3.4</DrawOdds>
  <AwayOdds>4.8</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>888</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:31.02</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.9</HomeOdds>
  <DrawOdds>3.4</DrawOdds>
  <AwayOdds>4</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Skybet</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:32.177</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.91</HomeOdds>
  <DrawOdds>3.4</DrawOdds>
  <AwayOdds>4</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>WilliamHill</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:29.91</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.91</HomeOdds>
  <DrawOdds>3.3</DrawOdds>
  <AwayOdds>4.33</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Bet365</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:30.02</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.83</HomeOdds>
  <DrawOdds>3.4</DrawOdds>
  <AwayOdds>4.5</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Expekt</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:30.13</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.82</HomeOdds>
  <DrawOdds>3.1</DrawOdds>
  <AwayOdds>4.6</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Betfred</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:30.473</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.83</HomeOdds>
  <DrawOdds>3.3</DrawOdds>
  <AwayOdds>4.33</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Bet-At-Home</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:31.13</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.82</HomeOdds>
  <DrawOdds>3.18</DrawOdds>
  <AwayOdds>4.25</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Interwetten</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:31.397</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.8</HomeOdds>
  <DrawOdds>3.4</DrawOdds>
  <AwayOdds>4</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>BWin</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:31.77</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.85</HomeOdds>
  <DrawOdds>3.3</DrawOdds>
  <AwayOdds>3.9</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Mybet</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:31.943</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.85</HomeOdds>
  <DrawOdds>3.3</DrawOdds>
  <AwayOdds>4</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>BetClic</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:32.067</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.82</HomeOdds>
  <DrawOdds>3.1</DrawOdds>
  <AwayOdds>4.6</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>188Bet</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:32.27</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.86</HomeOdds>
  <DrawOdds>3.15</DrawOdds>
  <AwayOdds>4.05</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>10Bet</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:32.473</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.83</HomeOdds>
  <DrawOdds>3.2</DrawOdds>
  <AwayOdds>4.4</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Sportingbet</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:32.707</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.83</HomeOdds>
  <DrawOdds>3.25</DrawOdds>
  <AwayOdds>4.33</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>NordicBet</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:30.63</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.84</HomeOdds>
  <DrawOdds>3.25</DrawOdds>
  <AwayOdds>4.25</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Ladbrokes</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:30.85</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.85</HomeOdds>
  <DrawOdds>3.1</DrawOdds>
  <AwayOdds>4.25</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>Unibet</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:30.38</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.9</HomeOdds>
  <DrawOdds>3.4</DrawOdds>
  <AwayOdds>4</AwayOdds>
</Odds>
<Odds>
  <FixtureMatch_Id>346076</FixtureMatch_Id>
  <Bookmaker>5Dimes</Bookmaker>
  <UpdatedDate>2015-06-20T19:42:31.507</UpdatedDate>
  <Type>1X2</Type>
  <HomeOdds>1.92</HomeOdds>
  <DrawOdds>3.4</DrawOdds>
  <AwayOdds>4.8</AwayOdds>
</Odds>
</OddsList>

我尝试使用此代码获取Bookmaker = Interwetten的所有内容:

$url = $this->buildUrl($name,$params);   
    $xml = '';

    $reader = new XMLReader();
    $reader->open($url);

    $dom = new DomDocument();
    $pathXML = array();
    while($reader->read() && $reader->nodeType == XMLREADER::ELEMENT && $reader->name == 'XMLSOCCER.COM'){
        $xml = simplexml_import_dom($dom->importNode($reader->expand(),true));

        $pathXML = $xml->xpath('//Odds[contains(Bookmaker,"Interwetten")]');


        /*if($this->getExpression()){
            $xml->xpath($this->getExpression());
        }*/
    }
    //$pls = $xml->xpath('/OddsList/Odds/Bookmaker');

    var_dump($pathXML);

但是var_dump让我回归

  

array(size = 0)空数组(size = 0)空数组(size = 0)为空   array(size = 0)为空

我需要使用XMLReader,因为我需要从xpath获取条件,而不是整个文档......

1 个答案:

答案 0 :(得分:0)

你的一会儿,似乎也没有被执行过一次。代码应该读取,直到需要的条件发生

 while($reader->read()) {
   if (reader->nodeType == XMLREADER::ELEMENT && $reader->name == 'XMLSOCCER.COM') ) {
        $xml = simplexml_import_dom($dom->importNode($reader->expand(),true));
        $pathXML = $xml->xpath('//Odds[contains(Bookmaker,"Interwetten")]');
        /*if($this->getExpression()){
            $xml->xpath($this->getExpression());
        }*/
        break;
    }
}