Java XML DOM parser example
July 19, 2010 21:58:34 Last update: July 23, 2010 21:37:23
Parsing XML in Java is really simple:
The parser implementation details are hidden behind the JAXP API. In case you want to know which parser implementation is used, this is what the JavaDoc for
The
import java.io.*; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public class DOMParserExample { public static void main(String[] args) throws Exception { if (args.length < 1) { System.out.println("Usage: java DOMParserExample <xmlFileName>"); return; } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); InputStream in = new FileInputStream(args[0]); Document doc = factory.newDocumentBuilder().parse(in); in.close(); System.out.println("DOM doc: " + doc); System.out.println("DOM doc class: " + doc.getClass().getName()); } }
The parser implementation details are hidden behind the JAXP API. In case you want to know which parser implementation is used, this is what the JavaDoc for
DocumentBuilderFactory.newInstance says:
- Use the
javax.xml.parsers.DocumentBuilderFactorysystem property.
- Use the properties file "
lib/jaxp.properties" in the JRE directory. This configuration file is in standardjava.util.Propertiesformat and contains the fully qualified name of the implementation class with the key being the system property defined above. Thejaxp.propertiesfile is read only once by the JAXP implementation and it's values are then cached for future use. If the file does not exist when the first attempt is made to read from it, no further attempts are made to check for its existence. It is not possible to change the value of any property injaxp.propertiesafter it has been read for the first time.
- Use the Services API (as detailed in the JAR specification), if available, to determine the classname. The Services API will look for a classname in the file
META-INF/services/javax.xml.parsers.DocumentBuilderFactoryin jars available to the runtime.
- Platform default DocumentBuilderFactory instance.
The
jaxp.debug system property also helps:
C:\local\utils>java -Djaxp.debug=1 DOMParserExample test.xml JAXP: find factoryId =javax.xml.parsers.DocumentBuilderFactory JAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl JAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl using ClassLoader: null DOM doc: [#document: null] DOM doc class: com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl