在PHP中显示xml子元素


displaying xml child elements in PHP

不是一个完整的n00b,但从未真正涉足XML

我有一个XML提要,当被查询时会返回此响应

<ContentAPI xmlns="http://www.geneity.co.uk/genbet/ContentAPI" status="OK" timezone="UTC" msg_stamp="NDczMTUwNjIyOjEwMDM6ZW4=" version="1.0" request="get_events_for_type">
<Sport sport_code="FOOT" name="Football" disporder="-1000">
<SBClass sb_class_id="12430" disporder="-999" name="United Kingdom">
<SBType sb_type_id="19157" name="Eng - Premier League" disporder="-1001">
<Ev inplay_allowed="Y" status="A" name="West Ham United v Liverpool" start_time="2016-01-02T12:45:00" virtual="N" ev_timezone="Europe/London" inplay_now="N" mkt_count="128" ev_id="3341306" disporder="-9999">
<EvDetail br_match_id="7464844"/>
<Teams>
<Team team_id="239" team_order="0" name="West Ham United" short_name="West Ham United"/>
<Team team_id="2577" team_order="1" name="Liverpool" short_name="Liverpool"/>
</Teams>
</Ev>
<Ev inplay_allowed="Y" status="A" name="Leicester City v Bournemouth" start_time="2016-01-02T15:00:00" virtual="N" ev_timezone="Europe/London" inplay_now="N" mkt_count="128" ev_id="3330641" disporder="-9999">
<EvDetail br_match_id="7464832"/>
<Teams>
<Team team_id="708" team_order="0" name="Leicester City" short_name="Leicester City"/>
<Team team_id="101178" team_order="1" name="Bournemouth" short_name="Bournemouth"/>
</Teams>
</Ev>
<Ev inplay_allowed="Y" status="A" name="Arsenal v Newcastle" start_time="2016-01-02T15:00:00" virtual="N" ev_timezone="Europe/London" inplay_now="N" mkt_count="127" ev_id="3341307" disporder="-9999">
<EvDetail br_match_id="7464826"/>
<Teams>
<Team team_id="96" team_order="0" name="Arsenal" short_name="Arsenal"/>
<Team team_id="1347" team_order="1" name="Newcastle" short_name="Newcastle"/>
</Teams>
</Ev>
<Ev inplay_allowed="Y" status="A" name="Manchester United v Swansea" start_time="2016-01-02T15:00:00" virtual="N" ev_timezone="Europe/London" inplay_now="N" mkt_count="127" ev_id="3341308" disporder="-9999">
<EvDetail br_match_id="7464834"/>
<Teams>
<Team team_id="2494" team_order="0" name="Manchester United" short_name="Manchester United"/>
<Team team_id="1351" team_order="1" name="Swansea" short_name="Swansea"/>
</Teams>
</Ev>
</SBType>
</SBClass>
</Sport>
</ContentAPI>

我使用代码

$xmlData = 'http://feeds-sports.winner.com/odds_feed?key=get_events_for_type&lang=en&&sb_type_id=19157';
$xml = simplexml_load_file($xmlData);
print $xml->Sport->attributes()->{'name'} .' - ';
print $xml->Sport->SBClass->attributes()->{'name'} .' <br /><br />';
print $xml->Sport->SBClass->SBType->attributes()->{'name'} .' <br />';
//print $xml->Sport->SBClass->SBType->Ev->attributes()->{'name'} .' <br />';
foreach ($xml->Sport->SBClass->SBType->Ev->Teams->Team as $team){
print $xml->Sport->SBClass->SBType->Ev->attributes()->{'name'} .' <br />';
//print $team->attributes()->{'short_name'} . ' vs ' . PHP_EOL;
}

它不会每次都打印新的记录——只打印同一个

任何帮助都将不胜感激。我现在只是拔头发,因为我知道这很容易

使用xml样本并添加关闭的Ev标签

<ContentAPI xmlns="http://www.geneity.co.uk/genbet/ContentAPI" status="OK" timezone="UTC" msg_stamp="MDoxOmVu" version="1.0" request="get_events_for_type">
<Sport sport_code="FOOT" name="Football">
<SBClass sb_class_id="12430" name="United Kingdom">
<SBType sb_type_id="19157" name="Barclays Premier League">
<Ev inplay_allowed="Y" status="A" name="Aston Villa v Norwich City" start_time="2012-10-27T11:45:00" ev_timezone="Europe/London" inplay_now="N" mkt_count="102" ev_id="26301">
<Teams>
<Team team_id="1323" team_order="0" name="Aston Villa" short_name="Aston Villa"/>
<Team team_id="136" team_order="1" name="Norwich City" short_name="Norwich City"/>
</Teams>
</Ev>
</SBType>
</SBClass>
</Sport>
</ContentAPI>

您可以按以下访问每个xml节点

$xml = simplexml_load_file('xml.xml');
print $xml->Sport->attributes()->{'name'} . PHP_EOL;
print $xml->Sport->SBClass->attributes()->{'name'} . PHP_EOL;
print $xml->Sport->SBClass->SBType->attributes()->{'name'} . PHP_EOL;
foreach ($xml->Sport->SBClass->SBType->Ev->Teams->Team as $team){
    print $team->attributes()->{'short_name'} . PHP_EOL;
}

哪个将输出

Football
United Kingdom
Barclays Premier League
Aston Villa
Norwich City