Document object model (DOM), level 2.
Simple API for XML (SAX), version 2.0
A simple Java API created by JDOM, Jason Hunter and Brett McLaughlin.
Java API(JAXP) for XML processing
The first three of these four interfaces (DOM, SAX
And JDOM) define how to access and represent the contents of XML documents. JAXP contains classes that create parser objects. To create a DOM or SAX parser, you need to use the.
JAXP. If you use JDOM, the JDOM library will use JAXP to create a parser for you behind the scenes. In short:
Use DOM, SAX or JDOM to process the contents of XML documents.
If DOM or SAX is used, JAXP is used to create the parser.
If you use JDOM, the JDOM library will create a parser for you.
I will study the design goals, advantages and disadvantages of each of the above APIs, and will also cover their history and the standard institutions that created these APIs.
About examples
This tutorial contains several sample programs using DOM, SAX and JDOM API. These programs mark XML with Shakespeare's sonnets. The structure of sonnets is as follows:
& lt Sonnets & gt
& lt author & gt
& lt surname & gt
& lt name & gt
& lt nationality & gt
& ltyearOfBirth & gt
& lt year of death & gt
& lt/ author & gt
& lt line & gt
[ 14 & lt; Line & gt element]
& lt/lines & gt;
& lt/sonnet & gt;
install a machine
Before running these examples, you need to make some settings for your machine.
Suppose you know how to compile and run Java programs and how to set classpath variables. )
First, please visit the Xerces XML parser home page on the Apache XML project (http://xml.apache.org/Xerces2-j/).
You can also download the page directly (http://xml.apache.org/xerces2-j/download.cgi).
Unzip the files downloaded from Apache. Depending on the parser version, this will create a directory named xerces-2_5_0 or something similar.
The required JAR files (xercesImpl.jar and xml-apis.jar) should appear in the root directory of Xerces.
Visit the JDOM project website and download the latest version of JDOM (http://jdom.org/).
Unzip the files downloaded from JDOM, which will create a directory named jdom-b9 or something similar.
The required JAR file (jdom.jar) should be in the build directory.
Finally, please download the sample compressed file of this tutorial and extract it.
Add the current directory (. ), xercesImpl.jar, xml-apis.jar and jdom.jar are added to the CLASSPATH variable.
basis
An XML parser is a piece of code that reads an XML document and analyzes its structure. This section will introduce how the XML parser works.
I will discuss different types of XML parsers and when to use them.
Later chapters in this tutorial will discuss how to create a parser and how to handle the results given by the parser.
How to use the parser
I will discuss this in detail in later chapters. Generally speaking, using a parser requires the following steps:
Create a parser object
Point the parser at the XML document.
Treatment outcome
Obviously, the third step is the most complicated. For example, once you know the content of an XML document, you may want to generate a web page, create an order or make a pie chart.
Considering the diversity of data contained in XML documents, it is an arduous task to write an application to handle all possible inputs.
Fortunately, the common XML parsing tools discussed here greatly simplify this work.