Parsing and serializing XML: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
m (added Resources and Sarissa reference)
mNo edit summary
Line 22: Line 22:
==Resources==
==Resources==
* [https://sourceforge.net/projects/sarissa/ Sarissa] - Sarissa is a cross-browser ECMAScript library for client side XML manipulation, including loading XML from URLs or strings, performing XSLT transformations, XPath queries and more. Supported: Gecko (Mozilla, Firefox etc), IE, KHTML (Konqueror, Safari).
* [https://sourceforge.net/projects/sarissa/ Sarissa] - Sarissa is a cross-browser ECMAScript library for client side XML manipulation, including loading XML from URLs or strings, performing XSLT transformations, XPath queries and more. Supported: Gecko (Mozilla, Firefox etc), IE, KHTML (Konqueror, Safari).
[[Category:Example code]] [[Category:XML in Javascript]]
[[Category:Example code]] [[Category:XML in JavaScript]]
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]]):
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]]):


Line 82: Line 82:
==Resources==
==Resources==
* [https://sourceforge.net/projects/sarissa/ Sarissa] - Sarissa is a cross-browser ECMAScript library for client side XML manipulation, including loading XML from URLs or strings, performing XSLT transformations, XPath queries and more. Supported: Gecko (Mozilla, Firefox etc), IE, KHTML (Konqueror, Safari).
* [https://sourceforge.net/projects/sarissa/ Sarissa] - Sarissa is a cross-browser ECMAScript library for client side XML manipulation, including loading XML from URLs or strings, performing XSLT transformations, XPath queries and more. Supported: Gecko (Mozilla, Firefox etc), IE, KHTML (Konqueror, Safari).
[[Category:Example code]] [[Category:XML in Javascript]]
[[Category:Example code]] [[Category:XML in JavaScript]]

Revision as of 02:07, 26 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. If you have already have a DOM tree from using XMLHttpRequest, skip to the end of this section.


Resources

  • Sarissa - Sarissa is a cross-browser ECMAScript library for client side XML manipulation, including loading XML from URLs or strings, performing XSLT transformations, XPath queries and more. Supported: Gecko (Mozilla, Firefox etc), IE, KHTML (Konqueror, Safari).

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.serializeToStream(doc, foStream, "");   // rememeber, doc is the DOM tree
foStream.close();

Serializing XMLHttpRequest objects to files

If you have already have a DOM tree from using XMLHttpRequest, use the same code as above but replace serializer.serializeToStream(doc, foStream, "") with serializer.serializeToStream(xmlHttpRequest.responseText, foStream, "") where xmlHttpRequest is an instance of XMLHttpRequest.

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");
// print the name of the root element or error message
dump(dom.documentElement.nodeName == "parsererror" ? "error while parsing" : dom.documentElement.nodeName);

Parsing files into DOM trees

XMLHttpRequest

As was previously mentioned, even though DOMParser does have a method named parseFromStream(), it's easier to use XMLHttpRequest to parse XML files into DOM trees (XMLHttpRequest works for both local and remote files). Here is sample code which reads and parses a local XML file into a DOM tree:

var req = new XMLHttpRequest();
req.open("GET", "chrome://passwdmaker/content/people.xml", false); 
req.send(null);
// print the name of the root element or error message
dump(dom.documentElement.nodeName == "parsererror" ? "error while parsing" : dom.documentElement.nodeName);

req.responseXML is a Document instance.

io.js

If you prefer io.js, this code will also parse a file into a DOM tree. Unlike XMLHttpRequest, it will not work with remote files:

var file = DirIO.get("ProfD"); // %Profile% dir
file.append("extensions");
file.append("{5872365E-67D1-4AFD-9480-FD293BEBD20D}");
file.append("people.xml");
var fileContents = FileIO.read(file);
var domParser = new DOMParser();
var doc = domParser.parseFromString(fileContents, "text/xml");
// print the name of the root element or error message
dump(dom.documentElement.nodeName == "parsererror" ? "error while parsing" : dom.documentElement.nodeName);

Resources

  • Sarissa - Sarissa is a cross-browser ECMAScript library for client side XML manipulation, including loading XML from URLs or strings, performing XSLT transformations, XPath queries and more. Supported: Gecko (Mozilla, Firefox etc), IE, KHTML (Konqueror, Safari).