Parsing and serializing XML: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
m (update links)
m (cat redirect)
 
(33 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Mozilla [https://bugzilla.mozilla.org/show_bug.cgi?id=155749 doesn't support] the W3C's [http://www.w3.org/TR/DOM-Level-3-LS/load-save.html Document Object Model Load and Save] at this moment, so the easiest way to serialize and deserialize DOM trees is to use the following Mozilla-specific interfaces:
Moved [http://developer.mozilla.org/en/docs/Parsing_and_serializing_XML here] (MDC).
 
[[Category:Redirects]]
*[http://unstable.elemental.com/mozilla/build/latest/mozilla/extensions/dox/interfacensIDOMSerializer.html nsIDOMSerializer] to serialize '''DOM trees to strings or to files'''
*[http://unstable.elemental.com/mozilla/build/latest/mozilla/extensions/dox/interfacensIDOMParser.html nsIDOMParser] to parse XML from '''strings into DOM trees'''
*[http://unstable.elemental.com/mozilla/build/latest/mozilla/extensions/dox/interfacensIXMLHttpRequest.html nsIXMLHttpRequest] to parse XML from '''files into DOM trees'''. Although <code>DOMParser</code> does have a method named <code>parseFromStream()</code>, it's actually easier to use [[XMLHttpRequest|XMLHttpRequest]] (yes, it works for local '''and''' remote files).
 
 
==Serializing DOM trees to strings==
First, create a DOM tree using code [[How to Create a DOM tree| like this]].
 
Now, let's serialize <code>doc</code> &mdash; the DOM tree &mdash; to a string:
 
<pre>
var serializer = new XMLSerializer();
var xml = serializer.serializeToString(doc);
</pre>
 
==Serializing DOM trees to files==
First, create a DOM tree using code [[How to Create a DOM tree| like this]].
 
Now, let's serialize <code>doc</code> &mdash; the DOM tree &mdash; to a file (you can read more about using files in Mozilla [[Dev : Extensions : Example Code : File IO | here]]):
 
<pre>
var serializer = new XMLSerializer();
var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
              .createInstance(Components.interfaces.nsIFileOutputStream);
var file = Components.classes["@mozilla.org/file/directory_service;1"]
          .getService(Components.interfaces.nsIProperties)
          .get("ProfD", Components.interfaces.nsIFile); // get profile folder
file.append("extensions");  // extensions sub-directory
file.append("{5872365E-67D1-4AFD-9480-FD293BEBD20D}");  // GUID of your extension
file.append("myXMLFile.xml");  // filename
foStream.init(file, 0x02 | 0x08 | 0x20, 0664, 0);  // write, create, truncate
serializer.serializeToFile(doc, foStream, "IS0-8859-1");  // rememeber, doc is the DOM tree
foStream.close();
</pre>
 
==Parsing strings into DOM trees==
<pre>
var theString='<a id="a"><b id="b">hey!</b></a>';
var parser = new DOMParser();
var dom = parser.parseFromString(theString, "text/xml");
</pre>
 
==Parsing files into DOM trees==
Coming soon
 
[[Category:Example code]] [[Category:XML in Javascript]]

Latest revision as of 14:57, 8 November 2006

Moved here (MDC).