I have an XML file as shown below:
<itemnumbers>
<item>
<itemno>123</itemno>
<desc>Desc about 123</desc>
</item>
<item>
<itemno>456</itemno>
<desc/>
</item>
...
</itemnumbers>
I would like to use the HTML5 localStorage to store the data (and retrieve for quicker access) since the XML data does not change regularly.
I am planning to convert it into JSON first and then store it in the localStorage. Should I do that in the code or have the data upfront in the .JSON file instead of the .xml file?
How do I parse the data later? Currently I am using jQuery code to parse...something like:
$(this).find("itemno").each(function()
{
$(this).text();
$(this).next().text()
}
Would the above code work after the JSON conversion?
I would like suggestions on the best way to approach this.
I have an XML file as shown below:
<itemnumbers>
<item>
<itemno>123</itemno>
<desc>Desc about 123</desc>
</item>
<item>
<itemno>456</itemno>
<desc/>
</item>
...
</itemnumbers>
I would like to use the HTML5 localStorage to store the data (and retrieve for quicker access) since the XML data does not change regularly.
I am planning to convert it into JSON first and then store it in the localStorage. Should I do that in the code or have the data upfront in the .JSON file instead of the .xml file?
How do I parse the data later? Currently I am using jQuery code to parse...something like:
$(this).find("itemno").each(function()
{
$(this).text();
$(this).next().text()
}
Would the above code work after the JSON conversion?
I would like suggestions on the best way to approach this.
Share Improve this question edited Nov 27, 2011 at 7:50 Josh Ferrara 7672 gold badges9 silver badges26 bronze badges asked Nov 27, 2011 at 7:15 copenndthagencopenndthagen 50.9k105 gold badges313 silver badges492 bronze badges 6-
5
XML is a string, JSON is a string,
localStorage
can hold strings. Why do you want to change the data format? – zzzzBov Commented Nov 27, 2011 at 7:17 - Bcoz of the lightweight format of JSON....XML, i think, takes longer time to parse... – copenndthagen Commented Nov 27, 2011 at 7:20
- 1 If it's in an XML file, why not just store the data in a .js (as JSON) file and be done with it? As far as parsing XML, JavaScript parses XML quite well and you'd be prematurely optimizing... – zzzzBov Commented Nov 27, 2011 at 7:21
- Well, I am open to that idea of storing directly as .json....But i wanted to know the details of how the parsing and all can be done... – copenndthagen Commented Nov 27, 2011 at 8:53
- XML and JSON are not equivocal formats. Your XML is pretty simple, so you will probably be fine, but normally you would loose some data integrity in moving from XML to JSON – austincheney Commented Nov 27, 2011 at 8:53
2 Answers
Reset to default 1I agree with some of the ments that you could just continue using XML. If you want to convert to JSON, you would use a For In loop in javascript to loop through it just as you would an object in javascript.
Your data in JSON:
{"itemnumbers":
{ "item": {"itemno": 123, "desc": "Desc about 123"} }
{ "item": {"itemno": 456, "desc": "Desc about 456"} }
}
Looping through your data where data is the JSON object above:
for (item in data.itemnumbers) {
//do something with item data
console.log(data.itemnumbers[item].itemno);
console.log(data.itemnumbers[item].desc);
}
To save an object in localStorage you must transform it to a string format that you can fetch back out again as an object. You can use JSON.stringify() to make an object a string and JSON.parse() to pull it back out:
//saving object to localStorage
localStorage['my_data'] = JSON.stringify(data);
//fetching object from localStorage
data = JSON.parse(localStorage['my_data']);
Beware because these methods aren't supported on IE7 and below so you'll need to find a parsing library patible with them. Here's a post that might help with patibility:
Safely turning a JSON string into an object
I would suggest you to write a script that converts the XML data into JSON and then send it over to the client side and save it.
Later parse the JSON when required, which is very easy to do. Just like the following :-
var jsonObject = JSON.parse(yourObjectInJSON);
Looping through it :-
for (item in jsonObject.itemnumbers) {
//do something with item data
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745104467a4611476.html
评论列表(0条)