在Javascript中循环遍历XML

时间:2015-04-05 20:26:38

标签: javascript xml xmlreader

我出于某种原因无法弄清楚如何遍历这个xml。我可以从第一个父节点和它下面的子节点获取数据,但不能让它转到第二个父节点。任何帮助都会很棒。我正在使用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如下:

<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>

1 个答案:

答案 0 :(得分:0)

我试图解析你的xml文件并发现我可以在浏览器中加载它,所以当我检查它时,我在第二个和第三个StateCrossword元素中发现了一个问题,这里是更新xml文件

<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>

我确实尝试用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];
          .............
        }
      }
    });