O'Reilly logo

Java and XML by Brett McLaughlin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using a Document

Once we have our initial Document object (either from instantiating one directly or building one using the JDOM input classes), we can act on the Document independently of any particular format or API. There are no ties to SAX, DOM, or the original format of the data. There is also no coupling to the output format, as we will see in the next section. Any JDOM Document object can be output to any format desired!

The Document object itself has methods that deal with the four components it can have: a DocType (referencing an external DTD, or providing internal definitions), ProcessingInstructions, a root Element, and Comments. Each of these objects maps to an XML equivalent, and provides a Java representation of those constructs in XML.

The Document DocType

The JDOM DocType object is a simple representation of a DOCTYPE declaration in an XML document. Assume we have the following XHTML file:

<!DOCTYPE html 
      PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <!-- etc -->
</html>

This code will print out the element, public ID, and system ID from the JDOM DocType object that maps to the declaration:

DocType docType = doc.getDocType(  );
  System.out.println("Element: " + docType.getElementName(  ));
  System.out.println("Public ID: " + docType.getPublicID(  ));
  System.out.println("System ID: " + docType.getSystemID(  ));

Its output is:

Element: html Public ID: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required