...

View Full Version : Problem parsing XML with PHP



willsko87
02-08-2010, 03:38 AM
Hi,

I'm looking to extract some specific information from an XML feed but I am having problems outputting values from similar tags.

Here is the XML:


<game_live>
<stats team="h">
<player name="A Player" g="0"/>
</stats>
<stats team="a">
<player name="B Player" g="0"/>
</stats>
If you look at the XML above the players separated into two teams using separate 'stats' tags.

Using the code below:


<?php
$xmlFileData = file_get_contents("match-10250605.xml");
$xmlData = new SimpleXMLElement($xmlFileData);

foreach($xmlData->stats as $stats1) {

$name = $stats1->player[0]["name"];

echo "Name: ".$name;

}

?>The output for this ends up being the names from the first player entry from both the stats tags. So in this case "A PlayerB Player".

If I put the echo outside the loop it only displays the second name "B Player".

How can I separate the two names into seperate variables? I have tried putting $xmlData->stats[0] instead of $xmlData->stats but it outputs nothing.

Cheers.

Fou-Lu
02-08-2010, 03:43 AM
The problem is just the $name. The foreach overwrites the value on each pass, which is why when you move the echo it only contains the last entry.
What you want is to create an array with name:


$name = array();
foreach($xmlData->stats as $stats1) {

$name[] = $stats1->player[0]["name"];
}


$name is now an array with each offset matching the name on each entry.
If you're needing more advanced handling with xml (which I don't really see in this example but...), look into using the DOMDocument instead. It is substantially more robust at the expense of complexity.

ninnypants
02-08-2010, 04:17 AM
You could use simplexml_load_string(). Your code worked when I used it. The SimpleXMLElement seems to only give you functions to get the data.

willsko87
02-08-2010, 04:33 AM
The problem is just the $name. The foreach overwrites the value on each pass, which is why when you move the echo it only contains the last entry.
What you want is to create an array with name:


$name = array();
foreach($xmlData->stats as $stats1) {

$name[] = $stats1->player[0]["name"];
}


$name is now an array with each offset matching the name on each entry.
If you're needing more advanced handling with xml (which I don't really see in this example but...), look into using the DOMDocument instead. It is substantially more robust at the expense of complexity.


Legend! That worked great thanks!




You could use simplexml_load_string(). Your code worked when I used it. The SimpleXMLElement seems to only give you functions to get the data.

Ok, where do I need to place that in my code?

ninnypants
02-08-2010, 06:32 AM
You would just replace your class call.


$xmlFileData = file_get_contents("match-10250605.xml");

$xmlData = simplexml_load_string($xmlFileData);

foreach($xmlData->stats as $stats1) {

$name = $stats1->player[0]["name"];

echo "Name: ".$name;

}

Fou-Lu
02-08-2010, 12:13 PM
simplexml_load_string returns a SimpleXMLElement object. You can use either for the same result as they are synonymous with each other.

ninnypants
02-08-2010, 05:01 PM
Good to know thanks.

Fou-Lu
02-08-2010, 06:02 PM
Good to know thanks.

np, I honestly cannot be 100% certain this is the case (including this post I've used simpleXML 3 times now, all for forum questions lol [I'm a DOM guy]), but went by what the API had mentioned. There is a difference though, if the constructor fails, I'll expect it to either toss an exception or return null, whilst if simplexml_load_* fails it should return false. That *may* be something that could help drive which you choose. Personally, I'm an OO guy, so I'd just stick with the constructors.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum