Portlet Factory, Version 6.1.2


 

About creating XML structures

To create XML structures, use the XMLUtil class methods create(), parseXml(), and parseHtml(). Alternatively, you can create a new XML structure by making a copy of an existing XML structure with the IXml().clone() method. You can 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 following code:

IXml myIXml = XmlUtil.create("Customers");
If you examine the myIXml object in the debugger, you see its structure consists of the following tag:
<Customers />

Otherwise, you can read in a file or string and create an IXML structure from it with the following code. For a string argument, use the XmlUtil.parseXml() method.

From an XML file

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.");
}

From an HTML file

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.");
}

From a string

IXml myIXml =
   XmlUtil.parseXml("<Customers><Customer><Name>Jina</Name></Customer></Customers>");

Parent topic: Overview: working with XML


Library | Support |