Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Problem parsing XML with PHP
Old 02-07-2010, 09:23 PM Problem parsing XML with PHP
Junior Talker

Posts: 2
Trades: 0
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:

Code:
<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:

Code:
<?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.
willsko87 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-08-2010, 05:26 AM Re: Problem parsing XML with PHP
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
This code only ever displays the name attribute of the first player
PHP Code:
$name $stats1->player[0]["name"]; 
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-08-2010, 06:59 AM Re: Problem parsing XML with PHP
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
This code only ever displays the name attribute of the first player
Yep, but the first player of each <stats> element is different..

I don't know simpleXml, but my guess is that it builds an array of the <player> tags, and don't keep them in sync with their parent.
Which makes it just rubbish if it is the case.

Try to run this:
PHP Code:
<?php 
$xmlFileData 
file_get_contents("match-10250605.xml");
$xmlData = new SimpleXMLElement($xmlFileData);
foreach(
$xmlData->stats as $stats1) {
    
var_dump($stats1);
}
?>
it will dump the structure of each <stats> node.
From there, we will be able tho see what happens.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 02-08-2010, 07:54 AM Re: Problem parsing XML with PHP
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Yep, but the first player of each <stats> element is different..
Good Point! Think I should go have another cuppa.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Problem parsing XML with PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14876 seconds with 12 queries