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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。