Section 21.5. Serializing XML


21.5. Serializing XML

It is sometimes useful to serialize an XML document (or some subelement of the document) by converting it to a string. One reason you might do this is to send an XML document as the body of an HTTP POST request generated with the XMLHttpRequest object. Another common reason to serialize XML documents and elements is for use in debugging messages!

In Mozilla-based browsers, serialization is done with an XMLSerializer object. In IE, it is even easier: the xml property of an XML Document or Element object returns the serialized form of the document or element.

Example 21-11 shows serialization code that works in Mozilla and IE.

Example 21-11. Serializing XML

 /**  * Serialize an XML Document or Element and return it as a string.  */ XML.serialize = function(node) {     if (typeof XMLSerializer != "undefined")         return (new XMLSerializer( )).serializeToString(node);     else if (node.xml) return node.xml;     else throw "XML.serialize is not supported or can't serialize " + node; }; 




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net