This section describes a tutorial example on how to create an XML file DOM parser using JAXP DocumentBuilder and DocumentBuilderFactory classes.
The most common use of JAXP is the parser interface and the DOM representation of XML documents.
If you want read XML a file and manipulate its contents as DOM object tree,
you need create a DOM parser instance, and use it to parse the XML file into a DOM Document object.
Here is simple tutorial example code called XmlDomFileParser.java
that parses an XML file and create a Document instance:
/*
* XmlDomFileParser.java
- Copyright (c) 2013, HerongYang.com, All Rights Reserved.
*/
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import java.io.File;
class XmlDomFileParser {
public static void main(String[] a) {
if (a.length<1) {
System.out.println("Usage:");
System.out.println("java XmlDomFileParser xml_file_name");
} else {
String name = a[0];
// parsing the XML file
Document document = parseXmlDom(name);
System.out.println();
System.out.println("Root Node Name: "
+ document.getChildNodes().item(0).getNodeName());
}
}
public static Document parseXmlDom(String name) {
Document document = null;
try {
// getting the default implementation of DOM builder
DocumentBuilderFactory factory
= DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
System.out.println();
System.out.println("Factory Class: "
+ factory.getClass().getName());
System.out.println("Builder Class: "
+ builder.getClass().getName());
// parsing the XML file
document = builder.parse(new File(name));
System.out.println();
System.out.println("Document Class: "
+ document.getClass().getName());
System.out.println("DOMImplement Class: "
+ document.getImplementation().getClass().getName());
} catch (Exception e) {
// catching all exceptions
System.out.println();
System.out.println(e.toString());
}
return document;
}
}
As the first test, let's try to parse the following simple XML file, first_html.xml:
<?xml version = "1.0" encoding = "utf-8"?>
<html>
<body>My first HTML document in XML format.</body>
</html>
The default implementation classes of the DocumentBuilderFactory, DocumentBuilderFactory, Document,
and DOMImplementation classes are all from the Apache Xerces project.
The root element name "html" shows that XML file was parsed correctly.
If you read the tutorial examples recorded in my other book
"JDK Tutorials - Herong's Tutorial Notes", you will see that JDK 1.4 was using a different set of default
implementation classes from the Apache Crimson project: