|
|
(39 intermediate revisions by 4 users not shown) |
Line 1: |
Line 1: |
| =Reading and Writing XML to and from DOM trees=
| | Moved [http://developer.mozilla.org/en/docs/Parsing_and_serializing_XML here] (MDC). |
| 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:
| | [[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 deserialize 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'''
| |
| | |
| 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==
| |
| First, create an XML document using code [[ How to Create an XML Document| like this]].
| |
| | |
| Now, let's serialize <code>doc</code> -- the DOM tree -- to a string:
| |
| | |
| <pre>
| |
| var serializer = new XMLSerializer();
| |
| var xml = serializer.serializeToString(doc);
| |
| </pre>
| |
| | |
| ==Serializing DOM trees to files==
| |
| First, create an XML document using code [[ How to Create an XML Document| like this]].
| |
| | |
| Now, let's serialize <code>doc</code> -- the DOM tree -- to a file:
| |
| | |
| <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/local;1"].createInstance(Components.interfaces.nsILocalFile);
| |
| file.initWithPath("ProfD"); // %Profile% directory
| |
| file.append("extensions"); // extensions 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>
| |
| | |
| ==Deserializing Strings into DOM Trees==
| |
| <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 parser = new DOMParser();
| |
| var dom = parser.parseFromString(theString, "text/xml");
| |
| </pre>
| |
| | |
| ==Deserializing Files into DOM Trees==
| |
| Coming soon
| |