简体   繁体   中英

XML, PHP & XPATH - no results

I'm having trouble using xpath and XML

This is an example of the XML file

    <OddService>
    <Header>
    <Status>000</Status>
    <Description>OK</Description>
    <Timestamp>1352407434</Timestamp>
    </Header>
    <Results>
    <Event>
    <EventID>1182745</EventID>
    <StartDate>2012-11-09T19:45:00</StartDate>
    <SportID>6046</SportID>
    <LeagueID>1182547</LeagueID>
    <LocationID>243</LocationID>
    <Status>NSY</Status>
    <LastUpdate>2012-11-08T20:27:14.767</LastUpdate>
    <HomeTeam ID="7" Name="Middlesbrough" Logo=""/>
    <AwayTeam ID="992" Name="Sheffield W" Logo=""/>
    <Scores status="" time=""/>
    <Scorers/>
    <Cards/>
    <Outcomes>...</Outcomes>
    </Event>
    <OddService>
    <Header>
    <Status>000</Status>
    <Description>OK</Description>
    <Timestamp>1352407434</Timestamp>
    </Header>
    <Results>
    <Event>
    <EventID>1182745</EventID>
    <StartDate>2012-11-09T19:45:00</StartDate>
    <SportID>6046</SportID>
    <LeagueID>1182547</LeagueID>
    <LocationID>243</LocationID>
    <Status>NSY</Status>
    <LastUpdate>2012-11-08T20:27:14.767</LastUpdate>
    <HomeTeam ID="7" Name="Middlesbrough" Logo=""/>
    <AwayTeam ID="992" Name="Sheffield W" Logo=""/>
    <Scores status="" time=""/>
    <Scorers/>
    <Cards/>
    <Outcomes>...</Outcomes>
    </Event>
    </Results>
    </OddService>

On this XML example, I've only included one of the entries. I'm using this PHP Code, but getting no results... does anyone have any ideas?

    <?php
    $loadfile = simplexml_load_file('matches.xml');

    foreach ($loadfile->OddService->Results as $gameinfo):
    $kodate=$gameinfo->StartDate;
    $status=$gameinfo->Status;
    $id=$gameinfo->EventID;
    $league=$gameinfo->LeagueID;
    $hometeam=$gameinfo->HomeTeam['Name'];
    $awayteam=$gameinfo->AwayTeam['Name'];
    $home=$gameinfo->Outcomes->Outcome->Bookmaker->Odds[0]['currentPrice'];
    $away=$gameinfo->Outcomes->Outcome->Bookmaker->Odds[1]['currentPrice'];
    $draw=$gameinfo->Outcomes->Outcome->Bookmaker->Odds[2]['currentPrice'];
    ?>

Thanks in advance!

该代码中没有xpath ...但是,当您加载XML时,根目录已经包含在内,因此您可能需要将foreach更改为

foreach ($loadfile->Results as $gameinfo):

I'm trying your XML file - after adding the root node myself - and it works OK. The only issue I see is that not all Results nodes have an Outcomes node, so you'll get an error when PHP runs the last three lines inside the foreach loop. You can fix that with this:

if ( $gameinfo->Outcomes ) {
    $home=$gameinfo->Outcomes->Outcome->Bookmaker->Odds[0]['currentPrice'];
    $away=$gameinfo->Outcomes->Outcome->Bookmaker->Odds[1]['currentPrice'];
    $draw=$gameinfo->Outcomes->Outcome->Bookmaker->Odds[2]['currentPrice'];
}

Other than that, it should work... Have you tried to var_dump 'ing $gameinfo inside the loop?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM