簡體   English   中英

在PHP中顯示xml子元素

[英]displaying xml child elements in PHP

並非總n00b,但從未真正涉足XML

我有一個XML feed,在查詢時會返回此響應

<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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM