To use the parser, set any required handlers for the XML document (addHandler() function), and then call one of the parse functions (parseFile(), parseURL() or parseString()) to parse the document. The parser will call a handler each time it finds an element that matches the handler pattern. If an object of the XML document is required then a pattern should be supplied for the built in handler and after the document has been parsed a compound script object starting at the element that matches the built in handler pattern will be available as the 'om' property in the parser.

XML Document Mapping

The default document mapping to the object model is:

For example the following XML document:

<a>
  <b att="an attribute">
    <c>First</c>
    <c>Second</c>
    <c>Third</c>
    <c>Fourth</c>
  </b>
  <d>Element d</d>
</a>

Maps to the following script object. Note that the c elements are returned as an array.

om.a
om.a.b
om.a.b.c[]
om.a.d

The 'om' variable is a container that holds the object model created and is not part of the object model.

Pattern Matching

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 document.

"/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.

e.g.

om.a
om.a.b
om.a.b.c[]
om.a.d

Using the example shown above:

Handler Functions

The handler function is called by the parser when an element matching the handler pattern is detected. This call is done when the end element is reached. This means that the element and all its child elements have been parsed and are available to the handler function.

The handler function is passed two or more parameters , the first parameter is a script compound object that represents the child elements of the matching node, the second parameter is a reference to the XMLParser that is being used to perform this parse, the remaining parameters are the optional parameters given at the end of the addHandler method call.

The handler function has a reference to the compound script object representing the matching element. Any changes made by the handler to the compound object are reflected in the final object returned by the parser. For example a handler could be used to make sure that an element is always returned as an array even if there is only one element in the XML source.

Parsing Example

For following XML document -

<a>
  <b att="an attribute">
    <c>First</c>
    <c>Second</c>
    <c>Third</c>
    <c>Fourth</c>
  </b>
  <d>Element d</d>
</a>

The following script will process the document:

function b_handler(b) {
  var attributes = b.getAttributes();
  if (attributes) {
    for (att in attributes) {
      writeln(“a.b.attribute."+ att + “ = “ +attributes[att] + “<br>" );
    }
  }
  if (b.c.isArray) {
    for(x=0; xb.c.length; x++) {
      writeln(“a.b.c[“ + x + “]= “ + b.c[x] + “<br>");
    }
  }
  else {
    writeln(“a.b.c = “ + b.c + “<br>" );
  }
}
 

var myParser = new XMLParser();
myParser.addHandler(“b", b_handler);
myParser.parseFile(“//c:/temp/a.xml", “/");
var myObjectModel = myParser.om;
if (myParser.error) {
  writeln(“The following error occurred during parsing “ + myParser.error);
  }
else {
  writeln((“a.d = “ + myObjectModel.a.d + “<br>" );
}

This would result in the following output

a.b.attribute.att = an attribute
a.b.c[0]= First
a.b.c[1]= Second
a.b.c[2]= Third
a.b.c[3]= Fourth
a.d = Element d

Comment on this topic

Topic ID: 150083