Use alternative XML representations
In this topic ...
Setting Up Your Environment to Use Alternative XML Objects
Processing Alternative XML Objects Stored in Variables
Creating Your Own XML Objects and APIs
Related Topics ...
The Factory provides support for other XML object models and APIs with functionality called Alternative XML Representation (AXR). AXR allows you to use, and convert, the following XML interfaces:
- IXml -- WebSphere Portlet Factory's interface for processing XML objects.
- Document Object Model (DOM ) -- The Worldwide Web Consortium's interface for processing objects in an XML document. For more information about DOM, see w3.org.
- JDOM -- A Java-oriented interface for processing objects in an XML document. For more information about JDOM, see jdom.org.
In addition, AXR allows you to define your own XML interfaces by providing a Java class representing the XML document and extending the com.bowstreet.util.ObjectBuilder and com.bowstreet.util.ObjectWalker classes.
If you define your own XML interfaces, you will need to add an ObjectRepresentation element to the ObjectRepresentations.xml file in the WEB-INF/config directory. The following XML structure shows the ObjectRepresentation element for the example Address XML interface:
<ObjectRepresentation>
<NamespaceURI>http://www.example.com/addresses</NamespaceURI>
<TypeName>Address</TypeName>
<ObjectClassName>com.bowstreet.examples.AlternativeXMLObject.Address</ObjectClassName>
<ObjectBuilderName>com.bowstreet.examples.AlternativeXMLObject.AddressBuilder</ObjectBuilderName>
<ObjectWalkerName>com.bowstreet.examples.AlternativeXMLObject.AddressWalker</ObjectWalkerName>
</ObjectRepresentation>
As a result of AXR functionality, we can set XML variables in your models to be either an IXml, DOM, or JDOM representation or a representation that you have created by developing a Builder and Walker for a class that represents some XML document.
Setting Up Your Environment to Use Alternative XML Objects
The Alternative XML Representation functionality requires that the .jar files containing a SAX parser and the DOM and JDOM classes be in the AutomationEngine's classpath. Also, any Builder and Walker classes must be in the classpath as well. By default, the AutomationEngine loads any classes found in the <nonservable_content_root>/classes directory at startup and dynamically loads classes found in the <nonservable_content_root>/work/classes directory.
Using JDOM and DOM Objects
We can create and process JDOM and DOM objects with the XmlUtil.createDOM() and XmlUtil.createJDOM() methods. You can create DOM or JDOM objects from a String, from an IXml object, or from a file (using a Reader object), just as you would create IXml objects. We can also convert objects from one object model to another. For example, you can convert the IXml object returned by a service to any other registered XML object type.
Refer to the JavaDoc for the XmlUtil class for more information about the createDOM() and createJDOM() methods and open the <nonservable_content_root>/work/source/AlternativeXMLObject.jpx project, which shows how to use DOM and JDOM objects in a model.
Processing Alternative XML Objects Stored in Variables
Unlike XML variables that contain IXML objects, we need to get and set alternative XML representations using the variables.getObject() or variables.setObject() methods. For example, if you configure an XML Data variable to be a DOM representation, to get the DOM object stored in that variable, you would use code similar to the following:
DOM myDomObject = (DOM)webAccess.getVariables().getObject("myDomVariable");
Note: You must cast to the appropriate object type when using the variables.getObject() method.
Once you retrieve the object, we can call the methods defined by the interface.
To set a variable to contain an alternative XML representation, use code similar to the following:
webAccess.getVariables().setObject("myDomVariable", myDomObject);
Note: The variable whose value you set must be configured to be the appropriate XML representation.
Create Your Own XML Objects and APIs
We can create your own XML representations for any XML document that you want to be able to create and process in your Web application models. For example, if you have a PurchaseOrder XML document that you want to use in your models, we can create a Java class that contains member variables representing the information stored in the Purchase Order and the accessor methods to those variables that provide for the getting and setting of the member values.
Once this class is created, you create a Walker class and a Builder class for it. The Builder class processes SAX events fired as the SAX parser processes the source XML. The Walker class fires SAX events to be processed by an XML Writer class with the end result being an XML representation of the PurchaseOrder object.
To use the new XML object, you could create an empty instance in a method, process the inputs from a page, and set the values of the new object's member variables with the input values. We can call any methods declared in the class you created for the new object, effectively creating your own API for the processing of PurchaseOrder objects. Typically, one of the methods declared in the new class handles the calling of the XML Writer class or any other processing that takes the output of the SAX parser and writes the new XML document.
Refer to the JavaDoc for the ObjectBuilder and ObjectWalker classes for more information about creating your own implementations of the ObjectBuilder and ObjectWalker classes. We can also open the <nonservable_content_root>/work/source/AlternativeXMLObject.jpx project, which shows how to create and use a custom XML object in a model.