Wednesday, March 12, 2008

XMLWriter for Javascript


This is a Javascript class, based on .NET's XMLTextWriter.
This is not a port, but a reduced and adapted version.


The constructor accepts 2 optional arguments: encoding, and version. You call it like this:
var xw = new XMLWriter( 'UTF-8', '1.0' );


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.


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.




Thatcher said...

Hi Ariel,
Just wondering why the default encoding isn't UTF-8?


Ariel Flesler said...

Hi Chris

I actually don't remember, I made the base of this script, long ago, dependant from a framework I made.

A few days ago, I decided to clean it up and make it standalone, but you are right, UTF8 would be more appropiate.

I'll make sure to change that in the trunk, will include it if I add another release.


alexandern said...

Good function class!

"Standalone" should be yes or no, I think.
And some escaping would be nice. I took the liberty to make this changes and make them available

Nijo Jose said...

How can read the value back from the xmlWriter?

Ariel Flesler said...

Hi, you can't. This is an XMLWriter, not a reader. If you need a value you just wrote, then keep it somewhere when you write it.

Anonymous said...

Hi Ariel,
   I need to delete the last added node... And I have added the  following method in your XMLWriter file. Its working  fine for me. Can you please tell me if its the right way of doing it?


Kathir said...

Hi Ariel,

First of all thanks for the nice tool

I need to save the generated xml as a file

is it possible ?

Please let me know ASAP


Ariel Flesler said...

Hi Kathir

For saving you need to use some workarounds, the same as you'd use to save any string.
Google "javascript write to disk"


hi can you please tell me how to run it and where will be the out put generated

Tatarize said...

It's dumbfounding how damned hard something to do this actually is. Apparently everybody and their mother wants to read XML, producing XML from JavaScript is a sadly overly rare concept.


Unknown said...

Ariel, help me please!!!. I'm new generating xml document. how to put it in a directory on the client disk?

kolen said...

Quick-fix for escaping:

Ariel Flesler said...

Hi Kolen
You should be using CDATA for writing html code, the default should not be to auto escape html.