在Javascript中循环使用XML
Loop through XML in Javascript
由于某种原因,我不知道如何循环使用这个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>
我试图解析你的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];
.............
}
}
});
相关文章:
- 在Javascript中循环使用XML
- 在一个循环中读取xml文件的所有节点
- for循环,直到xml属性不为null.从最后一行开始javascript
- javascript xml循环子节点
- 在分析 XML 文档后循环访问关联数组
- 通过单击foreach循环来显示/隐藏xml内容
- 从url解析xml文件并循环使用node.js获取其中的所有url
- 在JavaScript中使用for循环的XML数据表
- 在javascript中循环数组中的XML字符串
- 获取按钮,显示XML文件中循环的下一次迭代
- Javascript forEach在使用1个子节点循环JSON解析XML时不起作用
- 在javascript中循环XML并获取值
- 循环遍历XML文件jQuery
- Javascript -循环遍历xml元素并依次显示
- 循环遍历xml节点的所有属性
- 循环通过XML解析函数并在HTML中显示
- angular2ngRepeat循环遍历xml数据
- 如何在json到xml插件中使用循环
- 在Javascript中的循环中创建一个(xml)字符串
- 如何使用 Foreach 循环使用 ext/dom 输出 XML