This section provides a tutorial example on how to use the DOM interface included in JDK 1.4, which actually uses DOM implementation provided in the org.apache.crimson package.
Here is a program to show how different packages are used together to parse
an XML file into a DOM document object:
/**
* DOMParser.java
* Copyright (c) 2002 by Dr. Herong Yang. http://www.herongyang.com/
*/
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
class DOMParser {
public static void main(String[] args) {
try {
File x = new File(args[0]);
DocumentBuilderFactory f
= DocumentBuilderFactory.newInstance();
System.out.println(f.toString());
DocumentBuilder b = f.newDocumentBuilder();
System.out.println(b.toString());
Document d = b.parse(x);
System.out.println(d.toString());
DOMImplementation i = d.getImplementation();
System.out.println(i.toString());
} catch (ParserConfigurationException e) {
System.out.println(e.toString());
} catch (SAXException e) {
System.out.println(e.toString());
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
javax.xml.parsers.DocumentBuilderFactory.newInstance() method is used to
create a new factory instance using a factory implementation from the
org.apache.crimson.jaxp.* package.
javax.xml.parsers.DocumentBuilder.newDocumentBuilder() method is used to
create a new builder instance using a builder implementation from
org.apache.crimson.jaxp.* package.
javax.xml.parsers.DocumentBuilder.parse() method is used to
parse the XML file into an org.w3c.dom.Document object implemented with
org.apache.crimson.tree.XmlDocument class.
Notes and sample codes in this section are based on JDK 1.4.1_01.