Parsing and serializing XML: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
m (removed main title)
No edit summary
Line 1: Line 1:
Until Mozilla fully supports the W3C's [http://www.w3.org/TR/DOM-Level-3-LS/load-save.html Document Object Model Load and Save], the easiest way to serialize and deserialize DOM trees is to use the following interfaces:
Mozilla [http://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:
 


*[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/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 deserialize XML from '''strings into DOM trees'''
*[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 deserialize XML from '''files into DOM trees'''
*[http://unstable.elemental.com/mozilla/build/latest/mozilla/extensions/dox/interfacensIXMLHttpRequest.html nsIXMLHttpRequest] to parse XML from '''files into DOM trees''' ([[Using XMLHttpRequest|read more]]). Although [http://unstable.elemental.com/mozilla/build/latest/mozilla/extensions/dox/interfacensIDOMParser.html nsIDOMParser] does have a method named [http://unstable.elemental.com/mozilla/build/latest/mozilla/extensions/dox/interfacensIDOMParser.html#a1 parseFromStream()], it's actually easier to use [http://unstable.elemental.com/mozilla/build/latest/mozilla/extensions/dox/interfacensIXMLHttpRequest.html nsIXMLHttpRequest] (yes, it works for local '''and''' remote files).
 


Although [http://unstable.elemental.com/mozilla/build/latest/mozilla/extensions/dox/interfacensIDOMParser.html nsIDOMParser] does have a method named [http://unstable.elemental.com/mozilla/build/latest/mozilla/extensions/dox/interfacensIDOMParser.html#a1 parseFromStream()], it's actually easier to use [http://unstable.elemental.com/mozilla/build/latest/mozilla/extensions/dox/interfacensIXMLHttpRequest.html nsIXMLHttpRequest] (yes, it works for local '''and''' remote files).


==Serializing DOM trees to Strings==
==Serializing DOM trees to strings==
First, create a DOM tree using code [[ How to Create a DOM tree| like this]].
First, create a DOM tree using code [[How to Create a DOM tree| like this]].


Now, let's serialize <code>doc</code> -- the DOM tree -- to a string:
Now, let's serialize <code>doc</code> &mdash; the DOM tree &mdash; to a string:


<pre>
<pre>
Line 20: Line 17:


==Serializing DOM trees to files==
==Serializing DOM trees to files==
First, create a DOM tree using code [[ How to Create a DOM tree| like this]].
First, create a DOM tree using code [[How to Create a DOM tree| like this]].


Now, let's serialize <code>doc</code> -- the DOM tree -- to a file:
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>
<pre>
var serializer = new XMLSerializer();
var serializer = new XMLSerializer();
var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
              .createInstance(Components.interfaces.nsIFileOutputStream);
file.initWithPath("ProfD");   // %Profile% directory
var file = Components.classes["@mozilla.org/file/directory_service;1"]
file.append("extensions");  // extensions directory
          .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("{5872365E-67D1-4AFD-9480-FD293BEBD20D}");  // GUID of your extension
file.append("myXMLFile.xml");  // filename
file.append("myXMLFile.xml");  // filename
Line 37: Line 36:
</pre>
</pre>


==Deserializing Strings into DOM Trees==
==Parsing strings into DOM trees==
<pre>
<pre>
var theString="<people><person first-name="eric" middle-initial="h" last-name="jung"><address street="321 south st" city="denver" state="co" country="usa"/><address street="123 main st" city="arlington" state="ma" country="usa"/></person><person first-name="jed" last-name="brown"><address street="321 north st" city="atlanta" state="ga" country="usa"/><address street="123 west st" city="seattle" state="wa" country="usa"/><address street="321 south avenue" city="denver" state="co" country="usa"/></person></people>";
var theString='<a id="a"><b id="b">hey!</b></a>';
var parser = new DOMParser();
var parser = new DOMParser();
var dom = parser.parseFromString(theString, "text/xml");
var dom = parser.parseFromString(theString, "text/xml");
</pre>
</pre>


==Deserializing Files into DOM Trees==
==Parsing files into DOM trees==
Coming soon
Coming soon

Revision as of 13:03, 10 March 2005

Mozilla doesn't support the W3C's 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:


Serializing DOM trees to strings

First, create a DOM tree using code like this.

Now, let's serialize doc — the DOM tree — to a string:

var serializer = new XMLSerializer();
var xml = serializer.serializeToString(doc);

Serializing DOM trees to files

First, create a DOM tree using code like this.

Now, let's serialize doc — the DOM tree — to a file (you can read more about using files in Mozilla here):

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();

Parsing strings into DOM trees

var theString='<a id="a"><b id="b">hey!</b></a>';
var parser = new DOMParser();
var dom = parser.parseFromString(theString, "text/xml");

Parsing files into DOM trees

Coming soon