Getting specific data from xml -> php?

I’m pretty new at using XML but i’m trying :wink: I want to in 2 different foreach loops get data from xml below:

<?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

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

Well, actually I have gotton this far:

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

$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???

That is correct, you need to use $nchats->chat->attributes()->playerid to get the playerid attribute.