Lukeidiot Posted April 1, 2010 Share Posted April 1, 2010 Okay guys, I am trying to parse this XML data. I plan to read the xml, then add it to a mysql database. Here is the xml I am trying to parse: http://lines.bookmaker.com/ I tried to use this tutorial, but it only parses the stuff in the <test></test> tags http://www.ibm.com/developerworks/library/os-xmldomphp/ If you notice on http://lines.bookmaker.com/, it is all attributes. Any help/ideas are appreciated. Link to comment https://forums.phpfreaks.com/topic/197210-how-to-parse-this-xml-file-has-attributes/ Share on other sites More sharing options...
the182guy Posted April 1, 2010 Share Posted April 1, 2010 SimpleXML can read attributes, have a look at a tutorial. // get the raw XML into a SimpleXML object $xml = simplexml_load_file('http://lines.bookmaker.com/'); // get the IdLeague attribute echo "first league ID is: " . $xml->Leagues[0]->league[0]->attributes()->IdLeague; Link to comment https://forums.phpfreaks.com/topic/197210-how-to-parse-this-xml-file-has-attributes/#findComment-1035127 Share on other sites More sharing options...
Lukeidiot Posted April 1, 2010 Author Share Posted April 1, 2010 SimpleXML can read attributes, have a look at a tutorial. // get the raw XML into a SimpleXML object $xml = simplexml_load_file('http://lines.bookmaker.com/'); // get the IdLeague attribute echo "first league ID is: " . $xml->Leagues[0]->league[0]->attributes()->IdLeague; Thanks! That worked well, how would I automate this to make it get all the data automatically? Link to comment https://forums.phpfreaks.com/topic/197210-how-to-parse-this-xml-file-has-attributes/#findComment-1035136 Share on other sites More sharing options...
the182guy Posted April 1, 2010 Share Posted April 1, 2010 Say for example you wanted to retrieve all of the league data then you can just loop through it like: foreach($xml->Leagues[0] as $league) { echo "League ID: " . $league->attributes()->IdLeague; echo "League Sport: " . $league->attributes()->Description; echo "<hr/>"; } Link to comment https://forums.phpfreaks.com/topic/197210-how-to-parse-this-xml-file-has-attributes/#findComment-1035140 Share on other sites More sharing options...
Lukeidiot Posted April 1, 2010 Author Share Posted April 1, 2010 Say for example you wanted to retrieve all of the league data then you can just loop through it like: foreach($xml->Leagues[0] as $league) { echo "League ID: " . $league->attributes()->IdLeague; echo "League Sport: " . $league->attributes()->Description; echo "<hr/>"; } Excellent! What about the next one day? Like how would I get this? <league IdLeague="5" IdSport="MLB" Description="MAJOR LEAGUE BASEBALL"> <banner ab="True" vtm="MAJOR LEAGUE BASEBALL - Apr 01" htm=""/> <banner ab="False" vtm="EXHIBITION BASEBALL" htm=""/> − <game idgm="1034239" idgmtyp="3" gmdt="20100401" idlg="5" gmtm="09:05:00" idspt="MLB" vpt="ACTION" hpt="ACTION" vnum="971" hnum="972" vtm="FLA MARLINS" htm="STL CARDINALS" stats="false"> <line voddst="120" hoddst="-140" ovt="" ovoddst="" unt="" unoddst="" vsprdt="" vsprdoddst="" hsprdt="" hsprdoddst="" vspt="" vspoddst="" hspt="" hspoddst="" voddsh=" 2.20" hoddsh=" 1.71" vsprdh="" hsprdh="" ovh="" unh="" vsph="" hsph="" voddshr="0" vsprdhr="0" ovhr="0" vsphr="0" btot="False" bsprd="False" bml="False" haschild="False" evtyp="" related="True"/> </game> Link to comment https://forums.phpfreaks.com/topic/197210-how-to-parse-this-xml-file-has-attributes/#findComment-1035145 Share on other sites More sharing options...
the182guy Posted April 1, 2010 Share Posted April 1, 2010 Same thing applies e.g. just loop throgh the elements or access them directly if you know which one you want. foreach($xml->Leagues[0] as $league) { echo "League ID: " . $league->attributes()->IdLeague; echo "League Sport: " . $league->attributes()->Description; echo "<hr/>"; // loop through each banner in this league foreach($league->banner as $banner) { echo "banner vtm: " . $banner->attributes()->vtm; } // access the first game and line echo $league->game[0]->line[0]->attributes()->voddst; // or loop through each game foreach($league->game as $game) { $line = $game->line[0]; } } Link to comment https://forums.phpfreaks.com/topic/197210-how-to-parse-this-xml-file-has-attributes/#findComment-1035155 Share on other sites More sharing options...
Lukeidiot Posted April 1, 2010 Author Share Posted April 1, 2010 Same thing applies e.g. just loop throgh the elements or access them directly if you know which one you want. foreach($xml->Leagues[0] as $league) { echo "League ID: " . $league->attributes()->IdLeague; echo "League Sport: " . $league->attributes()->Description; echo "<hr/>"; // loop through each banner in this league foreach($league->banner as $banner) { echo "banner vtm: " . $banner->attributes()->vtm; } // access the first game and line echo $league->game[0]->line[0]->attributes()->voddst; // or loop through each game foreach($league->game as $game) { $line = $game->line[0]; } } Wow, you are good. Link to comment https://forums.phpfreaks.com/topic/197210-how-to-parse-this-xml-file-has-attributes/#findComment-1035161 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.