*
Metamerge logo
Search

Advanced Search
*
*
*
* HOME DOCUMENTS & RESOURCES DOWNLOADS EARLY TECH ACCESS SUPPORT FAQ KNOWN ISSUES OLD VERSIONS
*

 

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: The default parser will only permit you to read/write XML files two levels deep. It cannot read multivalued Attributes, even if it has written the file itself. See Examples below for an example of how to read multivalued Attributes.

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.

Examples

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
// Initialization done in After Select Entries hook


var nxt = list.item ( counter );

if ( nxt != null ) {
   var ch = nxt.getFirstChild();
   while ( ch != null ) {
      var child = ch.getFirstChild();
      while (child != null ) {
        // Use the grandchild's value if it exist, to be able to read multivalue attributes
	grandchild = child.getFirstChild();
	if (grandchild != null)
	  nodeValue = grandchild.getNodeValue();
     	else nodeValue = child.getNodeValue();
	// Ignore strings containing newlines, they are just fillers
     	if (nodeValue != null && nodeValue.indexOf('\n') == -1) {
       	    work.addAttributeValue ( ch.getNodeName(), nodeValue );
     	}
	child = child.getNextSibling();
      }
      ch = ch.getNextSibling();
   }
   
   result.setStatus (1); // Not end of input yet
   counter++;
} else {
   result.setStatus (0); // Signal end of input
}

The previous example parses files containing entries which look like this

<DocRoot>
  <Entry>
    <firstName>John</firstName>
    <lastName>Doe</lastName>
    <title>Engineer</title>
  </Entry>
  <Entry>
    <firstName>Al</firstName>
    <lastName">Bundy</lastName>
    <title">Shoe salesman</title>
  </Entry>
</DocRoot>

Suppose instead that the input looks like this:

<DocRoot>
  <Entry>
    <field name="firstName">John</field>
    <field name="lastName">Doe</field>
    <field name="title">Engineer</field>
  </Entry>
  <Entry>
    <field name="firstName">Al</field>
    <field name="lastName">Bundy</field>
    <field name="title">Shoe salesman</field>
  </Entry>
</DocRoot>

Here the attribute names can be retrieved from attributes of the "field" node, and we would use this code in the Override Getnext Hook

var nxt = list.item ( counter );

if ( nxt != null ) {
 var ch = nxt.getFirstChild();
 while ( ch != null ) {
  if(String(ch.getNodeName()) == "field") {
   attrName = ch.getAttributes().item(0).getNodeValue();
   nodeValue = ch.getFirstChild().getNodeValue();
   work.addAttributeValue ( attrName, nodeValue );
  }
  ch = ch.getNextSibling();
 }

 result.setStatus (1); // Not end of input yet
 counter++;
} else {
 result.setStatus (0); // Signal end of input
}

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.

Notes:

If you read big (> 4MB) or write big (>14MB) xml files, your Java VM will run out of memory.  Refer to the FAQ for a solution on this.

References:

See Also

SOAP Parser, DSML Parser

 

 

*
  Metamerge Integrator version 4.6 ©Copyright Metamerge AS 2000-2002 Last edited 2002-04-30 contact us