Jump to content

How to Parse this XML File (Has attributes)


Lukeidiot

Recommended Posts

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.

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;

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?

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/>";
}

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>

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

}

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.