Parsing XML in Scripts |
Active Web |
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.
The default document mapping to the object model is:
An XML element will be mapped to a script object with the same name.
Script objects will be added to their parent script objects.
If there is more than one script object at the same level of the same name then the objects will be returned as an array.
Any text encountered in an XML element will become the value of the object created.
Any attributes of an XML element are created as special properties of the script object created for the element. The attributes can only be accessed by calling the function getAttributes() on the script object.
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.
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:
The pattern is "b" or "a/b" or "/a/b" (these all refer to element b).
The handler function would be called with two parameters.
The first parameter would have no value but would have a property called "c" that would be an array containing four elements that would have the values "First", "Second", "Third" and "Fourth".
Calling getAttributes() on the parameter would return an object whose value would have a value of 1 and would contain one property whose name would be "att" and whose value would be "an attribute".
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.
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
Topic ID: 150083