- Forward


Processing XML in Java
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

The Java API for XML Processing (JAXP)
Back SMYC Forward
  • Purpose:
    • Parse and transform XML documents independent of a particular XML processing implementation
  • It Supports:
    • Element-Based Parsing
    • Document-Based Parsing
    • Document Transformations
Element-Based Parsing
Back SMYC Forward
  • Approach:
    • Process each element in the source XML document individually
    • Ignore the hierarchical nature of the XML document
  • Rationale:
    • Very convenient when the hierarchical structure is not important
Element-Based Parsing (cont.)
Back SMYC Forward
  • The Simple API for XML (SAX):
    • Uses the observer pattern
    • A SAXParser is the subject
    • Each observer is a DocumentHandler or a ContentHandler
  • Events:
    • startDocument and endDocument
    • startElement and endElement
    • characters
Element-Based Parsing (cont.)
Back SMYC Forward

A Simple Example

javaexamples/jaxp/AccidentReportHandler.java
 
Element-Based Parsing (cont.)
Back SMYC Forward

A Simple Example (cont.)

javaexamples/jaxp/AccidentReporter.java
 
Element-Based Parsing (cont.)
Back SMYC Forward

An Example that Uses Some Containment Properties

javaexamples/jaxp/DepartureTimeFinder.java
 
Element-Based Parsing (cont.)
Back SMYC Forward

An Example that Uses Some Containment Properties (cont.)

javaexamples/jaxp/DTFDriver.java
 
Document-Based Parsing
Back SMYC Forward
  • Approach:
    • Constructs a tree of objects from the XML document
  • Providing Access to the Tree:
    • Most common approach is to use the Document Object Model (e.g., DocumentBuilder )
    • Other approaches are possible
Document-Based Parsing (cont.)
Back SMYC Forward

An Example

javaexamples/jaxp/TimeTable.java
 
Transforming a Document
Back SMYC Forward
  • Approach:
    • Abstract away from the specific type of parsing
    • Think about transformation rules
  • Transformation Rules:
    • The most common approach is XSLT (e.g., Transformer )
Transforming a Document (cont.)
Back SMYC Forward

An Example

javaexamples/jaxp/XSLT.java
 
Validating a Document
Back SMYC Forward
  • The Idea:
    • Thus far, we have not imposed any structure on the XML document
  • Coming Soon:
    • In the future, we will talk about creating DTDs and schemas to impose structure
  • For Now:
    • You may be given a DTD or schema and want to validate an XML document against it
Validating a Document (cont.)
Back SMYC Forward

An Example

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(true); factory.setAttribute( "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); DocumentBuilder builder = factory.newDocumentBuilder(); // Then use the builder as before
There's Always More to Learn
Back -