Today's tip continues our discussion of parsing XML with Java using the JAXP parser from Sun. As discussed, JAXP supports both DOM and SAX. Today, we'll take a look at DOM.
As with any Java application, you start by importing the necessary packages:
import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException;
Unlike the SAX API, with DOM you do not have to use a URI to specify the
file to parse. Instead, you simply create an instance of the File class
and pass the filename to the constructor. For example, the following code
creates a file using a fictitious XML file located at /usr/john/test.xml:
File f = new File("/usr/john/test.xml");
Now, you're ready to start DOM'ing, which goes like this:
// get instance of document builder factory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // get instance of document builder from the factory DocumentBuilder db = docBuilderFactory.newDocumentBuilder(); // parser the document doc = db.parse (f); doc.getDocumentElement ().normalize ();
Voila! Hopefully, you've noticed that the boilerplate code for parsing a
document with either DOM or SAX is quite similar and rather simple. You
have only to retrieve an instance of a factory class and use that to create
a parser. Of course, things are seldom as simple as they first appear...
and in the next tip, we'll take a look at how to do more complex stuff with
the document once it has been parsed.