I have made a xml parser to parse some id (Player ID) from a xml--
var xml = evt.target.result;
var xmlDoc = $.parseXML(xml),
xml = $(xmlDoc),
playerId = xml.find("player_id").text(),
ids = playerId.match(/.{1,2}/g);
I am saving all the Player ids in a string and than slicing it into an array by two characters.
The xml i am parsing is this --
<?xml version="1.0" encoding="UTF-8" ?>
<player_info>
<general_info>
<team_name>Manchester United</team_name>
</general_info>
<player_segment>
<player_id>01</player_id>
<player_info>Ryan Giggs</player_info>
</player_segment>
<player_segment>
<player_id>02</player_id>
<player_info>Wayne Rooney</player_info>
</player_segment>
<player_segment>
<player_id>03</player_id>
<player_info>Zlatan Ibrahimovic</player_info>
</player_segment>
<player_segment>
<player_id>04</player_id>
<player_info>David de Gea</player_info>
</player_segment>
</player_info>
My parser is working fine for this xml and a live demo is here . Problem arises as there are possibilities of different sized (more than two characters) Player IDs. An example is given below --
<?xml version="1.0" encoding="UTF-8" ?>
<player_info>
<general_info>
<team_name>Manchester United</team_name>
</general_info>
<player_segment>
<player_id>012</player_id>
<player_info>Ryan Giggs</player_info>
</player_segment>
<player_segment>
<player_id>02</player_id>
<player_info>Wayne Rooney</player_info>
</player_segment>
<player_segment>
<player_id>0/3</player_id>
<player_info>Zlatan Ibrahimovic</player_info>
</player_segment>
<player_segment>
<player_id>04567</player_id>
<player_info>David de Gea</player_info>
</player_segment>
</player_info>
How can i handle such situations ?
xml.find("player_id").text()? What is the output when you printplayerId?.text()on multiple found xml elements, which concatenates them. Just loop through the elements and extract their content: jsfiddle.net/2ngyjdwn/5