Tuesday April 8
Download
Report
Transcript Tuesday April 8
MC365
XML Parsers
Today We Will Cover:
• An overview of the Java API’s used for XML
processing
• Creating an XML document in Java
• Parsing an XML document with SAX parsers
• Parsing an XML document with DOM
Overview of the Java API’s
• The main JAXP APIs are defined in the
javax.xml.parsers package that comes with the sdk 1.4.1.
– This package contains two vendor-neutral factory classes:
• SAXParserFactory and DocumentBuilderFactory that give you a
SAXParser and a DocumentBuilder, respectively.
• The DocumentBuilder, in turn, creates DOM-compliant Document
object.
• These factory APIs give you the ability to plug in an XML
implementation offered by another vendor without
changing your source code.
Creating an XML Document
• As we have seen, XML is a standard way of
communicating in the J2EE environment.
• To communicate a system needs to be able to both
create an XML document and process an XML
document.
• There are many ways to create this XML document
– Two popular options are:
• Use a text editor
• Create it In Java
– Here is an example of a Java application creating an XML
document:
http://www2.bc.edu/~bernier/MC365/Lecture Notes/XMLCreate.java
Parsing an XML Document in
Java
• Once you have created an XML document,
you need to be able to process/read it.
– To do this in Java, you use the Java parsers.
– You can use the parsers included in the sdk or
you can use a vendor-developed parser.
– There are two basic types of XML parser: SAX
and DOM
Overview of the Parsers
• SAX stands for The "Simple API" for XML
– This is an event-driven, serial-access mechanism that
does element-by-element processing.
– The API for this level reads and writes XML to a data
repository or the Web.
– Usually used for server-side and high-performance apps
Overview of the Parsers
• DOM stands for Document Object Model
• The DOM API is generally an easier API to use.
– It provides a relatively familiar tree structure of objects.
• You can use the DOM API to manipulate the hierarchy of
application objects it encapsulates.
• The DOM API is ideal for interactive applications because the
entire object model is present in memory, where it can be
accessed and manipulated by the user.
– On the other hand, constructing the DOM requires reading the
entire XML structure and holding the object tree in memory
• Makes it much more CPU and memory intensive.
• For that reason, the SAX API will tend to be preferred for
server-side applications and data filters that do not require an
in-memory representation of the data.
• Finally, the XSLT APIs defined in javax.xml.transform let you write
XML data to a file or convert it into other forms.
Overview of the Parser Packages
• The SAX and DOM APIs are defined by XML-DEV group and by the
W3C, respectively. The libraries that define those APIs are:
– javax.xml.parsers
• The JAXP APIs, which provide a common interface for
different vendors' SAX and DOM parsers.
– org.w3c.dom
• Defines the Document class (a DOM), as well as classes for all
of the components of a DOM.
– org.xml.sax
• Defines the basic SAX APIs.
– javax.xml.transform
• Defines the XSLT APIs that let you transform XML into other
forms.
Parsing an XML Document with
SAX Parsers
• Serial Access with the Simple API for XML (SAX)
– Use the SAX parser if the data structures have already been
determined, and you are writing a server application or an
XML filter that needs to do the fastest possible processing.
– Here is an example of a Java application using a SAX parser
to process an incoming XML document:
http://www2.bc.edu/~bernier/MC365/Lecture Notes/SAXexample.java
Parsing an XML Document with
DOM
• If you need to build an object tree from XML data
so you can manipulate it in an application, or
convert an in-memory tree of objects to XML.
• Here is an example Java application using DOM:
http://www2.bc.edu/~bernier/MC365/Lecture Notes/DomEcho02.java
Where to Get More Information
• There are many, many sites on XML, XML
Parsers, XSLT and other related technology.
• Some good ones are:
http://java.sun.com/xml/tutorial_intro.html
http://java.sun.com/webservices/docs/1.1/tutorial/doc/inde
x.html