General
This is a JavaScript class, based on .NET’s XMLTextWriter. This is not a port, but a reduced and adapted version. It allows you to easily generate XML. The script is heavily commented for complete understanding.
Notes
- Created by: Ariel Flesler
- Web Site: http://flesler.blogspot.com/2008/03/xmlwriter-for-javascript.html
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.
Source Code
Paste this source code into the designated areas.
External file
Paste this code into an external JavaScript file named: xmlWriter.js
/* This script and many more are available free online at
The JavaScript Source!! http://javascriptsource.com
Created by: Ariel Flesler | http://flesler.blogspot.com/2008/03/xmlwriter-for-javascript.htmlLicensed under: BSD License
*/
/**
* XMLWriter – XML generator for Javascript, based on .NET’s XMLTextWriter.
* Copyright (c) 2008 Ariel Flesler – aflesler(at)gmail(dot)com | http://flesler.blogspot.com
* Licensed under BSD (http://www.opensource.org/licenses/bsd-license.php)
* Date: 3/12/2008
* @version 1.0.0
* @author Ariel Flesler
* http://flesler.blogspot.com/2008/03/xmlwriter-for-javascript.html
*/
function XMLWriter( encoding, version ){
if( encoding )
this.encoding = encoding;
if( version )
this.version = version;
};
(function(){
XMLWriter.prototype = {
encoding:’ISO-8859-1′,// what is the encoding
version:’1.0′, //what xml version to use
formatting: ‘indented’, //how to format the output (indented/none) ?
indentChar:’t’, //char to use for indent
indentation: 1, //how many indentChar to add per level
newLine: ‘n’, //character to separate nodes when formatting
//start a new document, cleanup if we are reusing
writeStartDocument:function( standalone ){
this.close();//cleanup
this.stack = [ ];
this.standalone = standalone;
},
//get back to the root
writeEndDocument:function(){
this.active = this.root;
this.stack = [ ];
},
//set the text of the doctype
writeDocType:function( dt ){
this.doctype = dt;
},
//start a new node with this name, and an optional namespace
writeStartElement:function( name, ns ){
if( ns )//namespace
name = ns + ‘:’ + name;
var node = { n:name, a:{ }, c: [ ] };//(n)ame, (a)ttributes, (c)hildren
if( this.active ){
this.active.c.push(node);
this.stack.push(this.active);
}else
this.root = node;
this.active = node;
},
//go up one node, if we are in the root, ignore it
writeEndElement:function(){
this.active = this.stack.pop() || this.root;
},
//add an attribute to the active node
writeAttributeString:function( name, value ){
if( this.active )
this.active.a[name] = value;
},
//add a text node to the active node
writeString:function( text ){
if( this.active )
this.active.c.push(text);
},
//shortcut, open an element, write the text and close
writeElementString:function( name, text, ns ){
this.writeStartElement( name, ns );
this.writeString( text );
this.writeEndElement();
},
//add a text node wrapped with CDATA
writeCDATA:function( text ){
this.writeString( ‘