XML Parser
Overview
The XML parser reads and writes XML document. As of version 4.0.3 this parser now
uses the Apache Xerces and Xalan libraries
. The parser gives access to XML document through a script object called xmldom.
The xmldom is an instance of the org.w3c.dom.Document
interface. Please refer to http://java.sun.com/xml/jaxp-1.0.1/docs/api/index.html
for a complete description of this interface.
You can also use the XPathAPI (http://xml.apache.org/xalan-j/apidocs/index.html
and How to
Access Java Classes in your Scripts) to search and select nodes from
the XML document. selectNodeList, a convenience method in the system
object, can be used to select a subset from the XML document (see example
below).
When the Connector is initialised. the parser will try to perform DTD
verification if a DTD (Document Type Definition) tag is present.
Use the connector's override functions in order to interpret/generate the XML
document yourself. You do this by creating the necessary script in either the Override
getnext or Override add in your assembly line's hook definitions. If
you don't override the parser will read/write a very simple XML document that
mimics the entry object model: It
will only permit you to read/write XML files two levels deep. See Examples
below.
Note that certain methods, such as setAttribute are both available in
the Metamerge entry and the objects returned by xmldom.createElement.
These function just happen to have the same name/signature do not confuse
the xmldom objects with the Metamerge objects!
Configuration
Parameter |
Description |
class |
com.architech.parser.rspXml |
xmlRootTag |
The root tag (output) |
xmlEntryTag |
The entry tag for entries (output) |
xmlValueTag |
The value tag for entry attributes (output) |
characterSet |
Optional
character set conversion. |
isvalidating (4.6) |
If checked this parser will request a
DTD/Schema validating parser |
isnamespaceaware (4.6) |
If checked this parser will request a namespace
aware parser |
omitxmldeclaration (4.6) |
If checked, the XML declaration will be omitted
in the output stream. |
Override add hook
var root = xmldom.getDocumentElement();
var entry = xmldom.createElement ("entry");
var names = work.getAttributeNames();
for ( i = 0; i < names.length; i++ ) {
xmlNode = xmldom.createElement ("attribute");
xmlNode.setAttribute ( "name", names[i] );
xmlNode.appendChild ( xmldom.createTextNode ( work.getString( names[i] ) ) );
entry.appendChild ( xmlNode );
}
root.appendChild ( entry );
After Select Entries hook
//
// Set up variables for "override getnext" hook
//
var root = xmldom.getDocumentElement();
var list = system.selectNodeList ( root, "//Entry" );
var counter = 0;
Override getnext hook
//
// Note that the iterator hooks are NOT called when we override the getnext function
// Initialisation done in After Select Entries hook
var nxt = list.item ( counter );
if ( nxt != null ) {
var ch = nxt.getFirstChild();
while ( ch != null ) {
var nodeValue = ch.getFirstChild().getNodeValue();
if (nodeValue != null) {
entry.setAttribute ( ch.getNodeName(), nodeValue ); }
else { // no value, should be nested on next level
main.logmsg("skipping " + ch.getNodeName() );
// Insert code to parse further levels here
}
ch = ch.getNextSibling();
}
result.setStatus (1); // Not end of input yet
} else {
main.logmsg ("No more entries to read for iterator. Entries read: " + counter+1);
result.setStatus (0); // Signal end of input
}
counter++;
This example package demonstrates
how the base XMLParser functionality is extended to read XML more than two
levels deep, by using the "Override getnext" and "Override
add" hooks.
References:
See Also
SOAP Parser, DSML
Parser
|