[英]HTML5 sessionStorage and XMLDocument
I want to store an XML document in sessionStorage
. 我想将XML文档存储在
sessionStorage
。 When I do this it seems to save ok as an XMLDocument
. 当我这样做时,似乎可以将其另存为
XMLDocument
。 When I try to access it (on a page refresh) the object comes back as [object Object]
as opposed to [object XMLDocument]
. 当我尝试访问它(在页面刷新时)时,该对象以
[object Object]
而不是[object XMLDocument]
。
How do I get the data out of sessionStorage
as an XMLDocument
(or convert it to one?) 如何从
sessionStorage
获取数据作为XMLDocument
(或将其转换为XMLDocument
?)
localStorage
and sessionStorage
can only hold strings. localStorage
和sessionStorage
只能保存字符串。 Have a look at the interface definition : 看一下接口定义 :
interface Storage {
readonly attribute unsigned long length;
[IndexGetter] DOMString key(in unsigned long index);
[NameGetter] DOMString getItem(in DOMString key);
[NameSetter] void setItem(in DOMString key, in DOMString data);
[NameDeleter] void removeItem(in DOMString key);
void clear();
};
You cannot store objects unless you serialize them. 除非序列化对象,否则无法存储对象。 So in your case, you have to serialize it to XML .
因此,在您的情况下,您必须将其序列化为XML 。
If you received the XML document as text, just store it directly. 如果您以文本形式收到XML文档,则直接将其存储。 You can use
jQuery.parseXML()
to parse it after retrieval. 您可以在检索后使用
jQuery.parseXML()
进行解析。
You may want to serialize XMLDocument before saving it in storage: 您可能需要先序列化XMLDocument,然后再将其保存在存储器中:
var xml = new XMLSerializer().serializeToString(originalDom);
sessionStorage.setItem("myDocument", xml);
and then unserialize after loading data from storage: 然后从存储加载数据后反序列化:
var xml = sessionStorage.getItem("myDocument");
var restoredDom = new DOMParser().parseFromString(xml, "text/xml");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.