Using DOM Implementation Provided in JDK

This section provides a tutorial example on how to use the DOM interface included in JDK 1.8, 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-2014 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());      
      }
   }
}

If you compile and run this sample program with JDK 10 and 1.8, you will get:

C:\herong>java DOMParser hello.xml

com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl...
com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl@106d69c
[#document: null]
com.sun.org.apache.xerces.internal.dom.DeferredDOMImplementationIm...

Note that:



 

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

DOM (Document Object Model) Programming Interface

 What Is DOM (Document Object Model)?

Using DOM Implementation Provided in JDK

 DOM Specifications and DOM Node Interface

 DOMBrowser.java - DOM Interface Java Example

 XML DOM Node Tree Example

 Building a New DOM Document Object

 Converting DOM Document Objects to XML Files

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating XML Documents Against Specified XML Schemas

 XSL (Extensible Stylesheet Language) Introduction

 XSLT (XSL Transformations) Introduction

 Java Implementation of XSLT

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

 Control and Generate XML Element in the Result

 PHP Extensions for XML Manipulation

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML 1.1 Changes and Parsing Examples

 Outdated Tutorials

 References

 Full Version in PDF/EPUB