Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Problem parsing XML with PHP

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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:
    PHP Code:
    $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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    willsko87 (02-08-2010)

  • #3
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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:
    PHP Code:
    $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!



    Quote Originally Posted by ninnypants View Post
    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?
    Last edited by willsko87; 02-08-2010 at 04:48 AM.

  • #5
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    You would just replace your class call.
    PHP Code:
    $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;



  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    simplexml_load_string returns a SimpleXMLElement object. You can use either for the same result as they are synonymous with each other.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    ninnypants (02-08-2010)

  • #7
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    Good to know thanks.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by ninnypants View Post
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •