admin管理员组文章数量:1330565
I have an XML file like this:
<content>
<box>
<var1>A1</var1>
<var2>B1</var2>
<var3>C1</var3>
<var4>D1</var4>
</box>
<box>
<var1>A2</var1>
<var2>B2</var2>
<var3>C2</var3>
<var4>D2</var4>
</box>
<box>
<var1>A3</var1>
<var2>B3</var2>
<var3>C3</var3>
<var4>D3</var4>
</box>
</content>
It has 500 box
elements which I need to parse into JavaScript objects. I am using this code which works fine but I am a newbie and maybe I am missing something and would like to get suggestions if there is a better/faster way to do it:
var app = {
//...
box: [],
init: function (file) {
var that = this;
$.ajax({
type: "GET",
url: file,
dataType: "xml",
success: function (xml) {
$("box", xml).each(function (i) {
var e = $(this);
that.box[i] = new Box(i, {
var1: e.children("var1").text(),
var2: e.children("var2").text(),
var3: e.children("var3").text(),
var4: e.children("var4").text()
});
});
}
});
},
//...
};
Thanks in advance.
I have an XML file like this:
<content>
<box>
<var1>A1</var1>
<var2>B1</var2>
<var3>C1</var3>
<var4>D1</var4>
</box>
<box>
<var1>A2</var1>
<var2>B2</var2>
<var3>C2</var3>
<var4>D2</var4>
</box>
<box>
<var1>A3</var1>
<var2>B3</var2>
<var3>C3</var3>
<var4>D3</var4>
</box>
</content>
It has 500 box
elements which I need to parse into JavaScript objects. I am using this code which works fine but I am a newbie and maybe I am missing something and would like to get suggestions if there is a better/faster way to do it:
var app = {
//...
box: [],
init: function (file) {
var that = this;
$.ajax({
type: "GET",
url: file,
dataType: "xml",
success: function (xml) {
$("box", xml).each(function (i) {
var e = $(this);
that.box[i] = new Box(i, {
var1: e.children("var1").text(),
var2: e.children("var2").text(),
var3: e.children("var3").text(),
var4: e.children("var4").text()
});
});
}
});
},
//...
};
Thanks in advance.
Share Improve this question asked Jan 19, 2011 at 2:11 VerizonWVerizonW 1,8355 gold badges22 silver badges28 bronze badges3 Answers
Reset to default 3I have an XML source I am forced to use.. I convert it to JSON on the client side and then load it.. much easier..
Tracker.loadCasesFromServer = function () {
$.ajax({
type: 'GET',
url: '/WAITING.CASES.XML',
dataType: 'xml',
success: function (data) {
Tracker.cases = jQuery.parseJSON(xml2json(data, ""));
Tracker.loadCasesToMap();
},
data: {},
async: true
});
};
Used the XML2JSON converter that can be found here: http://www.thomasfrank.se/xml_to_json.html
Duncan
Use JSON if at all possible. That way the browser will do the parsing for you and you won't have to do any post-processing.
JSON from the server
{"content":
{"box": [
{"var1": "A1",
"var2": "B1",
"var3": "C1",
"var4": "D1"},
{"var1": "A2",
"var2": "B2",
"var3": "C2",
"var4": "D2"},
{"var1": "A3",
"var2": "B3",
"var3": "C3",
"var4": "D3"}]}}
Client JavaScript
var app = {
//...
box: [],
init: function (file) {
var that = this;
$.ajax({
type: "GET",
url: file,
dataType: "json",
success: function(result) {
that.box = $.map(result.content.box, function(box, i) {
return new Box(i, box);
});
}
});
},
//...
};
You can use browser native XML support which I guess would be fast. However this is varied amongst browsers e.g.(Firefox : DOMParser, IE: XMLDOM ..).
So instead of just going on and manually handling all browsers, you could make use of something like this https://sites.google./a/van-steenbeek/archive/explorer_domparser_parsefromstring
本文标签: Is this the fastest way to parse my XML into JavaScript objects using jQueryStack Overflow
版权声明:本文标题:Is this the fastest way to parse my XML into JavaScript objects using jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742274548a2444946.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论