отображение дочерних элементов xml в переполнении стека

Не полный 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;
}

Он не печатает новые записи каждый раз точно так же

Любая помощь будет принята с благодарностью. Я просто выдергиваю волосы, потому что знаю, что это легко

0

Решение

Используя ваш 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
1

Другие решения

Других решений пока нет …