To use these examples, don't forget to import the XML jars mentioned previously.
-
In TextXML.java, I basically put all the logic in one method. Yes,
I admit, this is very poor object oriented design. It should really
be broken up among multiple methods, and possibly multiple classes.
I also had an inner class to handle errors.
/*
* TestXML.java
*
* Created on May 8, 2003, 4:36 PM
*/
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
/**
*
* @author default
* @version
*/
public class TestXML {
/** Creates new TestXML */
public TestXML() {
}
public void runTest() {
try {
// First, read in data from XML file.
// We'll hardcode the filename now, but change it to an attribute later.
File f = new File("datasource.xml");
// Get a factory.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Tell it to validate.
factory.setValidating(true);
// Tell it to ignore whitespace.
factory.setIgnoringElementContentWhitespace(true);
// Get a builder from the factory.
DocumentBuilder builder = factory.newDocumentBuilder();
// Make an instance of the DatasourceErrorHandler.
DatasourceErrorHandler deh = new DatasourceErrorHandler();
// Add the ErrorHandler to the builder.
builder.setErrorHandler(deh);
// Parse the doucment
Document doc = builder.parse(f);
// Get the root element.
Element root = doc.getDocumentElement();
// Now get the children of the root element.
NodeList children = root.getChildNodes();
// loop through the elements.
for (int i=0; i < children.getLength(); i++) {
// Get the child as a node.
Node child = children.item(i);
// We shouldn't get any output here for our simple XML file,
// but just in case...
if (child instanceof Text) {
System.out.println("Text: " + ((Text) child).getData().trim());
} else if (child instanceof Element) {
// This is certainly not flexible. But we know from our XML
// that, if this is a child, it will only contain text.
// A better way to do this would be a recursive loop with an
// instanceof test.
Text textNode = (Text) child.getFirstChild();
System.out.println("TextNode: " + textNode.getData().trim());
}
}
// Informational.
System.out.println("Finished loop");
} catch (Exception e)
{
// Handle errors.
System.out.println("Exception in runTest. Message: " + e.getMessage());
e.printStackTrace();
}
}
public static void main(String args[]) {
// Create a TestXML object and run the test.
TestXML
txml = new TestXML();
txml.runTest();
}
class DatasourceErrorHandler implements org.xml.sax.ErrorHandler
{
public void error(org.xml.sax.SAXParseException
sAXParseException) throws org.xml.sax.SAXException {
System.out.println("OMG! Error." + sAXParseException.getMessage());
}
public void warning(org.xml.sax.SAXParseException
sAXParseException) throws org.xml.sax.SAXException {
System.out.println("Warning!" + sAXParseException.getMessage());
}
public void fatalError(org.xml.sax.SAXParseException
sAXParseException) throws org.xml.sax.SAXException {
System.out.println("OMG! Fatal Error!" + sAXParseException.getMessage());
}
}
}
-
The XML file is quite simple. I don't have any attributes, but feel
free to add some if you'd like.
<?xml version="1.0" ?>
<!DOCTYPE database SYSTEM "datasource.dtd">
<database>
<drivername>com.mysql.jdbc.Driver</drivername>
<databaseURL>jdbc:mysql://localhost/students</databaseURL>
</database>
-
And the DTD is equally simple.
<!ELEMENT database (drivername, databaseURL)>
<!ELEMENT drivername (#PCDATA)>
<!ELEMENT databaseURL (#PCDATA)>
-
Go ahead and try it out. Start from scratch, and refer to this or
ask me when you get stuck. If you have time, add some attributes.
SAX Parser
|