SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting specific data from xml -> php?

    I'm pretty new at using XML but i'm trying ;-) I want to in 2 different foreach loops get data from xml below:
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <data>
      <players>
        <player id="1">0</player>
        <player id="2">0</player>
      </players>
      <chats>
        <chat playerid="2">Some chat here...</chat>
      </chats>
    </data>
    I want to get the 2 players out in one foreach loop and get the chats in another and I want also to get the attributes?

    I have tried this but with no luck
    PHP Code:
    foreach($xml->children() as $players){
        echo 
    $players->player;
        
    // Not sure how to get the attribute?
        
    echo "<br />";
    }

    foreach(
    $xml->xpath('//chats') as $chat){
      echo 
    $chat"<br />";

    None of these examples gives any results? Please help...

    Thanks in advance :-)

  2. #2
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, actually I have gotton this far:
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <data>
      <players>
        <player id="1">4</player>
        <player id="2">3</player>
      </players>
      <chats>
        <chat playerid="2">Some chat here...</chat>
        <chat playerid="1">skfgjh kjgh fdskgjhdf kgjhdf gkjd gkjdfhg dkfjhg</chat>
      </chats>
    </data>
    I can get the first record, but only the first with this code:
    PHP Code:
    $userXML simplexml_load_file('xml_chats/0^0.xml') or die ("error"); 

    foreach(
    $userXML->chats as $nchats){
        echo 
    $nchats->chat.'<br>';    

    I get "Some chat here..."?

    Why???

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,162
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    That is correct, you need to use $nchats->chat->attributes()->playerid to get the playerid attribute.


Bookmarks

Posting Permissions

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