简体   繁体   中英

Loop through XML in Javascript

I for some reason can not figure out how to loop through this xml. I can get data out from the first parent nodes and the children nodes below it, but can not get it to go to the second parent node. Any help would be great. I'm using xmlReader.

function saveData(dataObject) {
        var GameData = Parse.Object.extend("AllGameData");
        var GameData = new GameData();

        dataObject.forEach(function (element, i) {
            GameData.set(element.name, element.data);
        });
        GameData.save();
    }    
Parse.Cloud.httpRequest({
url: xmlURL,
success: function (httpResponse) {
    var counter = 0;
    xmlreader.read(httpResponse.text, function (err, xmldata) {

        var state = xmldata.allcrosswords.StateCrossword.count();

        for(var y = 0; y < state.length; y++) {
            xmldata.allcrosswords.array[y].StateCrossword.array.forEach(function (element, i) {
                xmldata.allcrosswords.StateCrossword.array[i].game.array.forEach(function (element, j) {


                    saveData(data);
                    counter++;
                });
            });

        }
        status.success("" + counter + " row(s) inserted.");
    });
},
error: function (httpResponse) {
    status.error('Request failed with response code ' + httpResponse.status);
}

});

XML is below:

 <allcrosswords> <StateCrossword UserId="223943"> <game game_name="History CrossWord"> <crossword_date_played>02/01/2014</crossword_date_played> <words_wrong>2</words_wrong> <total_score>110</total_score> </game> </StateCrossword> <StateCrossword StateCrossword UserId="4894734"> <game game_name="Sports Crossword"> <crossword_date_played>04/16/2015</crossword_date_played> <words_wrong>10</words_wrong> <total_score>12</total_score> </game> </StateCrossword> <StateCrossword StateCrossword UserId="6092735"> <game game_name="Movies Crossword"> <crossword_date_played>08/04/2014</crossword_date_played> <words_wrong>12</words_wrong> <total_score>0</total_score> </game> </StateCrossword> </allcrosswords> 

I tried to parse you xml file and found out that I can load it in the browser, so when I examined it I found an issue in the second and third StateCrossword Elements, here is the update xml file

<allcrosswords>
  <StateCrossword UserId="223943">
    <game game_name="History CrossWord">
      <crossword_date_played>02/01/2014</crossword_date_played>
      <words_wrong>2</words_wrong>
      <total_score>110</total_score>
    </game>
  </StateCrossword>
  <StateCrossword UserId="4894734">
    <game game_name="Sports Crossword">
      <crossword_date_played>04/16/2015</crossword_date_played>
      <words_wrong>10</words_wrong>
      <total_score>12</total_score>
    </game>
  </StateCrossword>
  <StateCrossword UserId="6092735">
    <game game_name="Movies Crossword">
      <crossword_date_played>08/04/2014</crossword_date_played>
      <words_wrong>12</words_wrong>
      <total_score>0</total_score>
    </game>
  </StateCrossword>
</allcrosswords>

I did try to parse it with JQuery javascript,

$.ajax({
      type: "GET",
      url: "test.xml",
      dataType: "xml",
      success: function(data)
      {
        var StateCrosswords = data.getElementsByTagName("StateCrossword");
        for (var i=0; i < StateCrosswords.length; i++)
        {
          var  game = StateCrosswords.item(i).getElementsByTagName("game")[0];
          .............
        }
      }
    }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM