Home arrow JavaScript arrow More on JavaScript and XML
JAVASCRIPT

More on JavaScript and XML


In this conclusion to a three-part series, you'll learn about serializing XML, expanding HTML templates with XML data, and more. This article is excerpted from chapter 21 of JavaScript: The Definitive Guide, Fifth Edition, written by David Flanagan (O'Reilly; ISBN: 0596101996). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
August 16, 2007
TABLE OF CONTENTS:
  1. · More on JavaScript and XML
  2. · 21.6 Expanding HTML Templates with XML Data
  3. · 21.7 XML and Web Services
  4. · 21.8 E4X:ECMAScript for XML

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
More on JavaScript and XML
(Page 1 of 4 )

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;
};


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks
- Dynamic jQuery Styling

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials