ni8mr
ni8mr

Reputation: 1785

Parsing xml error

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 ?

Upvotes: 0

Views: 54

Answers (1)

arc
arc

Reputation: 4691

It should work like this.

All the nodes are found with this method: ids = xml.find("player_id") then you can access its content with ids[i].innerHTML as HTML is also just XML.

function handleFileSelect(evt) {
  var file = evt.target.files[0];
  if (file) {
    var reader = new FileReader();
    reader.readAsText(file, "UTF-8");
    reader.onload = function(evt) {
      var xml = evt.target.result;
      var xmlDoc = $.parseXML(xml),
        xml = $(xmlDoc),
        ids = xml.find("player_id")
      for (var i = 0; i < ids.length; i++) {
        if (i == 0) {
          $("#xml-data").empty().append($("<div>" + ids[i].innerHTML + "</div><br>"));
        } else {
          $("#xml-data").append($("<div>" + ids[i].innerHTML + "</div><br>"));
        }
      }
    }
  }
}

$("#file").change(handleFileSelect);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<title>XML file upload and parsing the title of the file</title>
<h2>Upload xml ...</h2>
<input type="file" id="file" accept=".xml" />
<div id="xml-data" style="color: red;"></div>

Upvotes: 1

Related Questions