XMLWriter: Simple JavaScript XML Creator

XML - a type of data defining - becoming more popular at present because of its flexibility and convenience, data defined by XML become more visual and clear to query with any platform. XML is being used by more and more web platforms/utilities such as: JavaScript, PHP, .NET, Java, Flash, etc.

Almost web development tools, web applications have exclusive libraries (functions, objects) to process XML data; besides; many famous platforms: WordPress, Zend, Joomla, etc also built its own libraries for XML; this is stable proof to proven above things jsB@nk said.

However, the field of XML is too big to discuss in this short post, because there was many professional books/e-books research it. Now in this post, jsB@nk like only to show you a simple JavaScript application for generating data in XML. Please go to the live demo of this JavaScript code example for short review.

Try more JavaScript XML parse scripts if you like:

- Random of Testimonial Bubbles with XML and jQuery
- getXML


Sampled by © JavaScriptBank.com

JS Code

var xw = new XMLWriter('UTF-8');
xw.formatting = 'indented';//add indentation and newlines
xw.indentChar = ' ';//indent with spaces
xw.indentation = 2;//add 2 spaces per level

xw.writeStartDocument( );
xw.writeDocType('"items.dtd"');
xw.writeStartElement( 'items' );

  xw.writeComment('button');
  xw.writeStartElement('item');
    xw.writeAttributeString( 'id', 'item-1');
    xw.writeAttributeString( 'enabled', 'true' );
    xw.writeStartElement( 'code');
      xw.writeCDATA( '<button>Save</button>' );
    xw.writeEndElement();
    xw.writeElementString('description', 'a save button');
  xw.writeEndElement();
  
  xw.writeComment('image');
  xw.writeStartElement('item');
    xw.writeAttributeString( 'id', 'item-2');
    xw.writeAttributeString( 'enabled', 'false' );
    xw.writeStartElement( 'code');
      xw.writeCDATA( '<img src="photo.gif" alt="me" />' );
    xw.writeEndElement();
    xw.writeElementString('description', 'a pic of myself!');
  xw.writeEndElement();
  
  xw.writeComment('link');
  xw.writeStartElement('item');
    xw.writeAttributeString( 'id', 'item-3');
    xw.writeAttributeString( 'enabled', 'true' );
    xw.writeStartElement( 'code');
      xw.writeCDATA( '<a href="http://google.com">Google</a>' );
    xw.writeEndElement();
    xw.writeElementString('description', 'a link to Google');
  xw.writeEndElement();
  
xw.writeEndElement();
xw.writeEndDocument();

Generated XML


Constructor

The constructor accepts 2 optional arguments: encoding, and version. You call it like this:

var xw = new XMLWriter( 'UTF-8', '1.0' );

Methods
Class instances have the following methods:

  • writeStartDocument([ bool standalone ])
    Opens a new document, must be call on start, if standalone is specified, standalone="true/false" will be added to the header.
  • writeEndDocument()
    Closes the active document, it's not really mandatory to call it.
  • writeDocType( string declarations )
    Adds a doctype to the document, can be called at anytime. If specified, a doctype will be included in the generated xml, in this form:
    <!DOCTYPE root-element declarations>
  • writeStartElement( string name [, string ns ] )
    Creates a new node element with this name, and it becomes the active element. A namespace can be specified.
  • writeEndElement()
    Closes the active element and goes up one level.
  • writeAttributeString( string attr, string value )
    Adds an attribute to the active element.
  • writeString( string text )
    Adds a text node to the active element.
  • writeElementString( string name, string txt [, string ns ] )
    Shortcut method, creates an element, adds the text and closes it.
  • writeCDATA( string text )
    Adds a text node wrapped with CDATA, to the active element.
  • writeComment( string text )
    Adds a comment node to the active element.
  • flush(): string
    Generates the XML string and returns it.
  • close()
    Closes the writer and cleans up.
  • getDocument()
    Generates a real XMLDocument from the writer. This method doesn't belong to the original class.

Formatting

You can choose whether the generated XML is formatted or not, and how. You can modify the following options for each instance or from XMLWriter.prototype to affect them all:

  • encoding 'ISO-8859-1' by default.
  • version '1.0' by default.
  • formatting 'indented'(default) or 'none'.
  • indentChar '\t' by default, char to indent.
  • indentation # of chars added per level, 1 by default.
  • newLine '\n' by default, char to separate lines.

If you choose formatting = 'none', you don't need to modify indentChar, indentation or newLine.


2000+ free JavaScripts
at www.JavaScriptBank.com