The XMLWriter Object

The XMLWriter object will map a script object model to an XML document.

For details on using the XMLWriter see Writing XML in Scripts.

It is possible to generate script objects that have properties which will not map into XML.  Under these circumstances the XMLWriter will generate invalid XML.  You should ensure that before rendering script objects to XML they do not contain characters which would generate invalid XML tag names.

For a list of the characters that can make up an XML tag refer to the XML 1.0 specification at http://www.w3c.org/ .

XMLWriter Object Properties

Property

Description

error

An error message If an error has occurred. If no error has occurred then it will be null. The error messages are explained below:

Insufficient number of parameters - The toFile or toString function has been called without the correct number of arguments.

Function not found for pattern - The addHandler function has been called and the function specified could not be found.

could not access properties - The toFile or toString function has been called and had a problem accessing the properties in the object model.

While accessing files the operating system can generate various other error messages.

CDATASection

This is a boolean property that is set to false by default. When set true, this has the effect of encapsulating element data in a CDATA section.

prototype

A reference to the Object Prototype. This property does not enumerate and can not be deleted, but it can be changed.

XMLWriter Object Functions

Function

Description

addHandler(pattern, function[, arg1[, . . .]])

Adds a handler function that will be called if an element is encountered during a write (toFile or toString) that matches the pattern supplied. The optional arguments are given to the handler function when it is called.

toFile(name, object)

Writes the object as an XML document to the file identified by the name. The name uses the script file reference to identify the file. File references outside the document root are allowed.

toString(object)

Returns the object as an XML document string.

removeHandler(pattern)

Removes all handlers that match the pattern.

Pattern Matching and Handler Functions

A pattern is a string which consists of one or more element names and optional selection criteria enclosed in square brackets separated by forward slashes.

e.g.

"/" will match the root element of the XML data.

"/a/b/c" will match all "c" elements whose parent is a "b" element and whose grandparent is the root element "a"

"a/b/c" will match all "c" elements whose parent is a "b" element and grandparent is the element "a" irrespective of where they occur in the document.

"c" will match all "c" elements irrespective of where they occur in the document. 

"b/c[2]" will match all the second c that has a parent of b

"b/c[2][@warning = 'true'] " will match all the second c that has an attribute named warning whose value is 'true' that has a parent of b

When a pattern is matched the handler function will be called. The function is passed two or more parameters , the first is a script object that represents the child elements of the matching node, the second is a reference to the XMLWriter that is being used to perform this write, the remaining parameters are the optional parameters given at the end of the addHandler method call.

The handler function must return the string representation of the script object.


The XMLWriter Constructor

Using the new Operator with the XMLWriter Constructor

The XMLWriter constructor is used to create new XMLWriter objects.

var myXMLWriter = new XMLWriter();

A new XMLWriter object is created. No arguments are required.

var myXMLWriter = new XMLWriter(CDATASection);

A new XMLWriter object is created using the argument supplied to determine whether element data should be placed in CDATA sections or not.

Shown below is the XMLWriter object prototype chain.

Calling the XMLWriter Constructor as a Function

When the constructor is run as a function without the new operator it has exactly the same behaviour as using the new operator.

XMLWriter Constructor Properties

Constructor properties are read only (they can not be changed or deleted) and it is not possible to add new properties to the Constructor.

Property

Description

prototype

A reference to the Object Prototype. This property does not enumerate and can not be changed or deleted.

XMLWriter Constructor Functions

The XMLWriter constructor has no predefined functions.

Comment on this topic

Topic ID: 150120