JDK Tutorials - Herong's Tutorial Examples - v6.32, by Herong Yang
DOMParser.java - Parsing XML Files with DOM
This section provides a tutorial example on how to write an XML file parser, DOMParser.java, with the org.w3c.dom.Document class included in JDK.
What is DOM (Document Object Model)? DOM is an Application Programming Interface (API) that represents an XML file as a document object, which allows application programs to manage the information contained in the document object.
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) HerongYang.com. All Rights Reserved. */ 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()); } } }
Now let's use this program to parse my first XML file, hello.xml:
<?xml version="1.0"?> <body>Hello world!</body>
Output with JDK 1.8 or higher:
herong> java DOMParser.java hello.xml com.sun.org.apache.xerces.internal.jaxp .DocumentBuilderFactoryImpl@1db9742 com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl@106d69c [#document: null] com.sun.org.apache.xerces.internal.dom .DeferredDOMImplementationImpl@52e922
Note that:
If you are using JDK 1.4.1, the output should look like:
herong> \j2sdk1.4.1_01\bin\java DOMParser hello.xml org.apache.crimson.jaxp.DocumentBuilderFactoryImpl@1c78e57 org.apache.crimson.jaxp.DocumentBuilderImpl@13e8d89 org.apache.crimson.tree.XmlDocument@1cfb549 org.apache.crimson.tree.DOMImplementationImpl@1820dda
Table of Contents
Date, Time and Calendar Classes
Date and Time Object and String Conversion
Number Object and Numeric String Conversion
Locales, Localization Methods and Resource Bundles
Calling and Importing Classes Defined in Unnamed Packages
HashSet, Vector, HashMap and Collection Classes
Character Set Encoding Classes and Methods
Encoding Conversion Programs for Encoded Text Files
Datagram Network Communication
►DOM (Document Object Model) - API for XML Files
►DOMParser.java - Parsing XML Files with DOM
DOMBrowser.java - Browsing DOM Tree Structure
DOMNewDoc.java - Building a New DOM Document
DOMToXML.java - Converting DOM Documents to XML Files
DTD (Document Type Definition) - XML Validation
XSD (XML Schema Definition) - XML Validation
XSL (Extensible Stylesheet Language)
Message Digest Algorithm Implementations in JDK
Private key and Public Key Pair Generation
PKCS#8/X.509 Private/Public Encoding Standards
Digital Signature Algorithm and Sample Program
"keytool" Commands and "keystore" Files
KeyStore and Certificate Classes
Secret Key Generation and Management
Cipher - Encryption and Decryption
The SSL (Secure Socket Layer) Protocol
SSL Socket Communication Testing Programs