Java XML DOM parser example 

Joined:
04/09/2007
Posts:
753

July 19, 2010 21:58:34    Last update: July 23, 2010 21:37:23
Parsing XML in Java is really simple:
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.DocumentBuilderFactory system property.
  • Use the properties file "lib/jaxp.properties" in the JRE directory. This configuration file is in standard java.util.Properties format and contains the fully qualified name of the implementation class with the key being the system property defined above. The jaxp.properties file 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 in jaxp.properties after 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.DocumentBuilderFactory in 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


Share |
| Comment  | Tags