英文:
How to Serialize and Deserialize an XMLDocument in Javascript?
问题
我在document.adoptNode
上遇到了错误,错误信息为'parameter1 is not valid node'。
以下是我的代码。
$.ajax({
type: "GET",
url: url,
datatype: "xml",
success: function (results) {
//results是一个XMLDocument类型的对象,其中包含SVG信息
console.log(results);
//需要将results序列化以存储在localStorage中
localStorage.setItem('results', JSON.stringify(results));
var results2 = JSON.parse(localStorage.getItem('results'));
var svgnode = $('svg', results);
var origwidth = svgnode.attr('width');
var origheight = svgnode.attr('height');
var docnode = document.adoptNode(svgnode[0]);
}
});
我需要序列化和反序列化以将其存储在localStorage中。如何序列化和反序列化XMLDocument?
英文:
I am getting error on document.adoptNode
as 'parameter1 is not valid node'.
Below is my code.
$.ajax({
type: "GET",
url: url,
datatype: "xml",
success: function (results) {
//results is a XMLDocument type object which contains a SVG information
console.log(results);
//need to serialize results to store it in localStorage
localStorage.setItem('results', JSON.stringify(results));
var results2 = JSON.parse(localStorage.getItem('results'));
var svgnode = $('svg', results);
var origwidth = svgnode.attr('width');
var origheight = svgnode.attr('height');
var docnode = document.adoptNode(svgnode[0]);
}
});
I need serialization and deserialization to store it in localStorage. How can i serialize and deserialize XMLDocument?
答案1
得分: 2
XML有自己的文本格式,我不会使用JSON进行序列化/反序列化,我会使用XML。
在你的ajax调用中,你正在要求jQuery为你反序列化接收到的XML文本。但是,由于你想要使用该文本,我会将dataType
更改为"text"
,这样你将收到一个包含XML的字符串。然后将其存储在localStorage
中,并在需要XMLDocument
而不是字符串时进行解析。
大致如下:
$.ajax({
type: "GET",
url: url,
dataType: "text",
success: function(results) {
// `results` 是一个字符串
localStorage.setItem('results', results);
// 无需重新获取它,你已经在`results`中拥有它了;解析它
var results2 = new DOMParser().parseFromString(results, "text/xml");
var svgnode = $('svg', results);
var origwidth = svgnode.attr('width');
var origheight = svgnode.attr('height');
var docnode = document.adoptNode(svgnode[0]);
}
});
或者你可以使用jQuery来解析它:
$.ajax({
type: "GET",
url: url,
dataType: "text",
success: function(results) {
// `results` 是一个字符串
localStorage.setItem('results', results);
// 无需重新获取它,你已经在`results`中拥有它了;解析它
var svgnode = $.parseXML(results);
var origwidth = svgnode.attr('width');
var origheight = svgnode.attr('height');
var docnode = document.adoptNode(svgnode[0]);
}
});
如果你需要再次从XMLDocument
转换为字符串,可以像这样做:
var xmlString = new XMLSerializer().serializeToString(xmlDocument);
英文:
XML has its own text format, I wouldn't use JSON for the serialization/deserialization, I'd use XML.
In your ajax call, you're asking jQuery to deserialize the XML text received for you. But since you want to use that text, I'd change dataType
to "text"
so you receive a string with the XML in it. Then store that in localStorage
and parse it when you need an XMLDocument
instead of a string.
Something along these lines:
$.ajax({
type: "GET",
url: url,
datatype: "text",
success: function(results) {
// `results` is a string
localStorage.setItem('results', results);
// No need to re-get it, you already have it in `results`; parse it
var results2 = new DOMParser().parseFromString(results, "text/xml");
var svgnode = $('svg', results);
var origwidth = svgnode.attr('width');
var origheight = svgnode.attr('height');
var docnode = document.adoptNode(svgnode[0]);
}
});
Or you can parse it with jQuery instead:
$.ajax({
type: "GET",
url: url,
datatype: "text",
success: function(results) {
// `results` is a string
localStorage.setItem('results', results);
// No need to re-get it, you already have it in `results`; parse it
var svgnode = $.parseXML(results);
var origwidth = svgnode.attr('width');
var origheight = svgnode.attr('height');
var docnode = document.adoptNode(svgnode[0]);
}
});
If you ever need to go from an XMLDocument
to a string again, you can do that like this:
var xmlString = new XMLSerializer().serializeToString(xmlDocument);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论