dounao2829 2015-12-31 17:56
浏览 59
已采纳

在PHP中显示xml子元素

Not a total n00b but never really dabbled with XML

I have an XML feed which when queried returns this response

<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>

Im using the code

$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;
}

Its not printing new records each time just the same one

Any help would be hugely appreciated. I'm just pulling my hair out at the minute because I know its something easy

  • 写回答

1条回答 默认 最新

  • duanguan1573 2015-12-31 18:34
    关注

    Using your xml sample and adding a closing Ev tag

    <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>
    

    You can access each xml node as follows

    $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;
    
    }
    

    Which will output

    Football
    United Kingdom
    Barclays Premier League
    Aston Villa
    Norwich City
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题