The example programs include a cross-platform ant build file that
can be used to build and run the example. Ant is a build tool
Sample XML FilesA handful of sample XML files have been provided in the "samples" subdirectory. Note that the links may not work depending on your browser environment. Please look in the "examples/samples" directory if the links do not display in your browser.
Printing a DOM Tree
One of the first things many programmers want to know is how to read an XML file and generate a DOM Document object from it. Use the DOMEcho example to learn how to do this in three steps. The important lines are:
// Step 1: create a DocumentBuilderFactory and setNamespaceAware DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); // Step 2: create a DocumentBuilder DocumentBuilder db = dbf.newDocumentBuilder(); // Step 3: parse the input file to get a Document object Document doc = db.parse(new File(filename));
The program also gives an example of using an error handler and of setting optional configuration options, such as validation. Finally, this program allows you to understand the DOM itself, because it