Chapter 15
612
The locateDealsXML() method performs all the necessary co-ordination needed, by making the
query via the getDeals() method of queryBean. It also creates an instance of DOMBuild, and uses
the AddATrip() method to add more nodes to the DOM tree.
public String locateDealsXML(int tripno) {
DOMBuild myDOM;
ByteArrayOutputStream myOut = new ByteArrayOutputStream();
String retVal = "";
try {
myDOM = new DOMBuild();
myQuery.getDeals(""+tripno);
myDOM.CreateRoot();
while (myQuery.getNextTrip()) {
myDOM.AddATrip(myQuery.getColumn("tripno"),
myQuery.getColumn("region"),
myQuery.getColumn("startdate"),
myQuery.getColumn("duration"),
myQuery.getColumn("location"),
myQuery.getColumn("price"),
myQuery.getColumn("commission"));
}
Finally, it asks DOMBuild to write the tree out as an XML document in a string:
myDOM.writeDOMTree(new PrintWriter(myOut));
retVal = myOut.toString();
} catch (Exception ex) {
retVal = ex.toString();
}
return retVal;
}
}
RDBMS Query Result to XML: DOMBuild and DOMWriter Classes
The DOMBuild and DOMWriter classes make extensive use of the Apache Xerces library. Here is the
source code to DOMBuild.java. This class builds the DOM tree consisting of a root <sell-off>
element, with a collection of <trip> subelements.
package com.wrox.pjxml;
import java.io.IOException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xerces.parsers.DOMParser;
import java.io.PrintWriter;