Processing XML with IXml
In this topic ...
Related Topics ...
Most objects that you work with in the Factory represent, or interact with, an XML structure. WebSphere Portlet Factory's API for working with the XML structures representing models, variables, form inputs, etc. is the IXml interface, which contains all the methods we need to add, delete, modify, and select elements in an XML structure.
Create XML Structures
To create XML structures, use the XMLUtil class' create(), parseXml(), and parseHtml() methods. Alternatively, we can create a new XML structure by making a copy of an existing XML structure with the IXml().clone() method.
Create an Empty Structure
When you are trying to build up a new XML structure based on form inputs, or any other time you want to create a structure before you populate it with elements, use the XmlUtil.create() method, as shown in the code below:
IXml myIXml = XmlUtil.create("Customers");
If you examine the myIXml object in the debugger, you will see its structure consists of:
<Customers />
Create a Structure from an XML File
We can read in an XML file and create an IXML structure from it with the following code:
IXml myIXml=null;
try {
FileReader fr = new FileReader(pathVar + "file.xml");
BufferedReader br = new BufferedReader(fr);
myIXml = XmlUtil.parseXml(br);
}
catch (IOException ioe) {
System.out.println(ioe+"Cannot find file.");
}
catch (com.bowstreet.util.parser.ParserException pe) {
System.out.println(pe+"Cannot parse file.");
}
Create a Structure from an HTML File
We can read in an HTML file and create an IXml structure from it with the following code:
IXml myIXml=null;
try {
FileReader fr = new FileReader(pathVar + "file.html");
BufferedReader br = new BufferedReader(fr);
myIXml = XmlUtil.parseHtml(br);
}
catch (IOException ioe) {
System.out.println(ioe+"Cannot find file.");
}
catch (com.bowstreet.util.parser.ParserException pe) {
System.out.println(pe+"Cannot parse file.");
}
Create a Structure from a String
We can create an IXml structure by passing in a String argument to the XmlUtil.parseXml() method, as shown in the code below:
IXml myIXml =
XmlUtil.parseXml("<Customers><Customer><Name>Jina</Name></Customer></Customers>");
Add Elements
We can add elements to an IXml structure by retrieving the node to which you want to add an element and calling one of the following IXml methods:
- addChildElement(String) -- Adds an empty element named according to the String passed. For example, the following code adds an existing IXml structure (myIXml):
IXml childElement = myIXml.addChildElement("Customer");
- addChildElement(IXml) -- Adds the passed IXml structure as a child of the current node. For example, you could copy a node from one IXml structure to another with the following code:
myIXml.addChildElement(otherIXml.findElement("parent/child").cloneElement());
- addChildWithText(String, String) -- Adds an element named according to the first String passed with a value of the second String passed. For example, you could create an element <Customer> with a text value of "Marco" with the following code:
myIXml.addChildWithText("Customer", "Marco");
Modify Elements
We can modify an element in an IXml structure by changing its name, text value, or by adding attributes and setting attribute values. Use the following methods to perform these tasks:
- setName(String) -- Use this method to change the name of the current IXml element.
- setText(String) -- Use this method to set the text value of the current IXml element.
- setText(String, String) -- Use this method to set the text value of the IXml element specified by the XPath supplied as the first String argument to the value of the String passed as the second argument.
- setAttribute(String, String) -- Use this method to set the attribute specified by the first String argument to the value of the second String argument. To add an attribute to the element, call this method with the value of the first String argument being the name of the attribute you want to add.
Removing Elements
We can remove elements from an IXml structure by calling one of the following methods:
- removeChildElement(IXml) -- Use this method to remove the IXml structure passed as the argument from the current IXml structure.
- removeChildElement(String) -- Use this method to remove the element specified by the path passed as a String argument from the current IXml structure.
- removeChildren() -- Use this method to remove all the child elements of the current element.
Iterating Over Elements
We can iterate over a number of child elements, processing each element with code similar to the following:
for(IXml classInfo = classesInfo.getFirstChildElement();
classInfo != null;
classInfo = classInfo.getNextSiblingElement()) {
System.out.println(classInfo.getText());
}