I am trying to get 11 xml elements from a 'n' number of xml elements. I have 11 ids where id
is unique for all xml elements.
I am using php
$xml = simplexml_load_string($j[0]); // $j[0] contains 'n' number of xml elements. I need to get 11 xml elements with 11 different ids.
$doc = new DOMDocument();
$doc->loadXML($j[0]);
$players = $doc->getElementsByTagName('player');
I need to parse through the below xml data($j[0]
)
<testsquad>
<team name="team1" abbr="t1" >
<tplayers>
<player name="player1" id="1"/>
<player name="player2" id="2"/>
<player name="player3" id="3"/>
<player name="player4" id="4"/>
<player name="player5" id="5"/>
<player name="player6" id="6"/>
<player name="player7" id="7"/>
<player name="player8" id="8"/>
<player name="player9" id="9"/>
<player name="player10" id="10"/>
<player name="player11" id="11"/>
<player name="player12" id="12"/>
<player name="player13" id="13"/>
<player name="player14" id="14"/>
<player name="player14" id="14"/>
</tplayers>
</team>
</testsquad>
From the given xml data with 'n' number of player elements, I need to get only 11 elements.
I am confused with searching through xml data using php.
When I do the var_dump($doc->loadXML($j[0]));
, I got the below error.
Warning: DOMDocument::loadXML(): Empty string supplied as input
Where I am going wrong?. Please comment if my question is not clear.
The given xml data doesn't have the header like below.
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
Is that the reason, it is saying Empty string supplied as input?
I used SimpleXMLElement's xpath instead of DOMDocument and I got the Simple XML Element object as a result.
Check out the below code.
$player_id_list = array(1,3,5,7,9,11,13,15,17,19,21);
$xml=new SimpleXMLElement($j[0]);
foreach($player_id_list as $new_player_id)
{
$result = $xml->xpath("team/PlayerDetails/player[@id=".$new_player_id."]");
echo "<pre>";
var_dump($result);
echo "</pre>";
}