gpt4 book ai didi

javascript - 在 Javascript 中循环 XML

转载 作者:行者123 更新时间:2023-11-28 07:27:39 24 4
gpt4 key购买 nike

出于某种原因,我不知道如何循环这个 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 - 在 Javascript 中循环 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29461865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com