简体   繁体   English

HTML5 sessionStorage和XMLDocument

[英]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. localStoragesessionStorage只能保存字符串。 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.

相关问题 HTML5 sessionStorage 未按预期工作 - HTML5 sessionStorage not working as expected 用户可以禁用 html5 sessionStorage 吗? - Can user disable html5 sessionStorage? HTML5 localStorage/sessionStorage中如何存储对象 - How to store objects in HTML5 localStorage/sessionStorage html5文件api,将用户选择的目录存储在sessionStorage中? - html5 file api, storing user selected directory in sessionStorage? 如何使用HTML5中的sessionStorage概念在Javascript中打印对象数据 - How to print object data in Javascript using sessionStorage concepts in HTML5 html5 SessionStorage在android模拟器中不起作用 - html5 SessionStorage doesn't work in android emulator HTML5 sessionStorage 是否有提供良好兼容性实现的包装器? - Is there a wrapper for HTML5 sessionStorage that provides a good compatability implementation? HTML5 sessionStorage:变量未存储在 <li> onclick属性 - HTML5 sessionStorage : variable not stored on <li> onclick attribute HtmlUnit和jasmine-maven-plugin是否支持HTML5 sessionStorage? - Does HtmlUnit and jasmine-maven-plugin support HTML5 sessionStorage? 如何在javascript sessionstorage / HTML5中动态创建数组 - How to dynamically create an array within a javascript sessionstorage / HTML5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM