Using Portlet Factory's IXml and XmlUtil Interfaces
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.
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:
<?xml version="1.0" encoding="utf-8"?
<service xmlns="http://purl.org/atom/app#" xmlns:atom="http://www.w3.org/2005/Atom"
<workspace
<atom:title type="text"
Teamspace Documents</atom:title
<collection href="http://serv/dm/atom/library/111/feed"
xmlns="xxx"
<atom:title type="text" xmlns:atom="http://serv/2005/Atom"
HR
Docs</atom:title
<accept
application/*,image/*,*/*</accept
</collection
<collection href="http://serv/dm/atom/library/222/feed"
xmlns="xxx"
<atom:title type="text" xmlns:atom="http://serv/2005/Atom"
Sam's
Library</atom:title
<accept
application/*,image/*,*/*</accept
</collection
...
</workspace
</service
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:
<RowSet
<Row
<label
</label
<value
</label
</Row
<RowSet
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:
<Libraries
<Library
<label
My Library</label
<value
http://server/dm/atom/library/123/feed</value
</Library
...
</Libraries
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 < /li>
- IXml.addChildWithText() is us ed to create Xml elements
with text values.
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();
while(collections.hasNext())
{
IXml collection = (IXml) collections.next();
String colFeedUrl = collection.getAttribute("href");
String colTitle = collection.findElement("atom:title").getText();
IXml lib = XmlUtil.create("Library");
lib.addChildWithText("label", colTitle);
lib.addChildWithText("value", colFeedUrl);
libs.addChildElement(lib);
}
return libs;
Installing the Sample
- 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.
Reference
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.