0

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 ?

8
  • 1
    Isn't this already achieved by calling xml.find("player_id").text()? What is the output when you print playerId? Commented Feb 18, 2017 at 10:29
  • I am facing problem in getting proper player Id from second xml. I am searching an universal/unique solution, which work irresponsible of the length of player Id. Commented Feb 18, 2017 at 10:31
  • MDN: Regular Expressions Commented Feb 18, 2017 at 10:31
  • @Andreas ... i am already using regex. Commented Feb 18, 2017 at 10:32
  • 1
    The problem is you were calling .text() on multiple found xml elements, which concatenates them. Just loop through the elements and extract their content: jsfiddle.net/2ngyjdwn/5 Commented Feb 18, 2017 at 10:37

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.