Java Reference
In-Depth Information
The
DOMParserBean
referenced in the example of
Listing 17-11
is shown in
Listing 17-12
. Again, this
class is a simple extension of the
XBean
base class. Its
getXml()
method returns a byte array
containing an XML document read from a file or a URL or simply passed as a
String
. The
processDocument()
method uses a Xerces
DOMParser
to convert the byte array to a DOM
representation of the document. The DOM document is then sent to the
DOMListener
defined in the
XBean
base class, using a
DOMEvent
.
Listing 17-12: DOMParserBean
package JavaDatabaseBible.ch17.Xbeans;
import java.io.*;
import java.net.*;
import org.Xbeans.*;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.apache.xerces.parsers.DOMParser;
public class DOMParserBean extends JavaDatabaseBible.ch17.Xbeans.XBean{
protected String UrlString = null;
protected String XmlString = null;
protected String XmlFileName = null;
public DOMParserBean(){
}
public void setXmlString(String XmlString){
this.XmlString = XmlString;
}
public void setUrlString(String UrlString){
this.UrlString = UrlString;
}
public void setXmlFileName(String XmlFileName){
this.XmlFileName = XmlFileName;
}
public void processDocument(){
try{
byte[] xml = getXml();
if(xml.length > 0){
ByteArrayInputStream x = new ByteArrayInputStream(xml);
DOMParser p = new DOMParser();
InputSource s = new InputSource(x);