IXml and XMLUtil
Portlet Factory's IXml and XmlUtil interfaces are lightweight and powerful API's for working with XML data. An integral component of Portlet Factory internals, they are useful for creating, parsing, and setting various XML data.
This article and sample application demonstrates how you can use these API's to manipulate XML data in your application. The application will parse an ATOM feed and create a new XML structure.
The new XML is used to populate a Select input on a page, as shown in the following image.
The sample application contains one model, IXmlSample.model.
The atomFeed variable holds the ATOM feed XML to be parsed. The ATOM feed is based on a Lotus Quickr REST service that returns a feed of document libraries:
The Select builder adds a Select input on the page. A Select builder can easily get its data from IXml variable in the following general form:
Following this general form, the model contains a Variable builder, libraries, that is used for the Select builder's "Select Data" input. The variable's Type input is set to XML. Populated at runtime, libraries will contain data similar to this:
When the Action List main is called, the libraries variable is set to the return value of getLibraries method. And here is where the fun begins!
The getLibraries method uses IXml to parse the ATOM feed and create a new Xml variable suitable for the Select list.
Several XmlUtil and IXml methods are called:
XmlUtil.create() is used to create the elements in our new XML structure
IXml.findElement() is used to navigate, or skip down, to a particular place in the ATOM feed
IXml.getChildren() is used to get a List of child IXml elements.
IXml.getAttribute() is used to read in the value of an XML attribute
IXml.findElement() is used to find a subelement of an XML structure
IXml.getText() is used to retrieve the text value of an element
IXml.addChildWithText() is us ed to create Xml elements with text values.
Installing the Sample
// Create new <Libaries> XML structure IXml libs = XmlUtil.create("Libraries");
// Get var that holds the ATOM feed
IXml atomFeed = webAppAccess.getVariables().getXml("atomFeed");
// Skip down to the <collections> elements
IXml collectionsXml = atomFeed.findElement("service/workspace");
// For each lib, parse the title & feed url. Create a <Library> structure
// that contains the title and value. Add the <Library> structure to
// the <Libaries> structure
Iterator collections = collectionsXml.getChildren().iterator();
IXml collection = (IXml) collections.next();
String colFeedUrl = collection.getAttribute("href");
String colTitle = collection.findElement("atom:title").getText();
IXml lib = XmlUtil.create("Library");
Create a new WebSphere Portlet Factory project.
Import the following zip as a WebSphere Portlet Factory archive IXmlSample.zip
The zip contains one model: models\samples\IXmlSample.model.
For more information, see the IXml and XmlUtil javadoc that ships with Portlet Factory. This is accessible by select Start> All Programs> IBM WebSphere> Portlet Factory> Documentation> Java API Documentation.