This chapter is one of the longest in this book and deals with many APIs and concepts. Part of the reason for this is that there has been a great deal of evolution of XML tools over time in order to support working with XML at different levels of abstraction. We’re going to introduce you to the APIs that we think remain important and useful in this chapter and to some extent we’ll do this by starting at the bottom level and working our way up. First we’ll cover basic XML concepts and low-level APIs such as the event-driven SAX (Simple API for Java) and model-building DOM (Document Object Model). We’ll also discuss related technologies such as XML Schema validation, XPath queries, and XSL (Extensible Style Sheet) transformation. Later in this chapter, we’ll discuss the higher-level JAXB, Java XML Binding API, for mapping plain Java objects directly to XML and back.
This means that for some of you, the most useful material may be
toward the end of this chapter where we cover the high-level tools. So we
want to reassure you that things get more interesting as the chapter
progresses. When we reach the section on JAXB we’ll see that we can take
plain old Java objects (POJOs) and write them to XML by adding (in the
simplest case) a one-line annotation. The following snippet shows a Java
Person
and Address
class and the corresponding XML that
they would map to by default.
@XmlRootElement
public
class
Person
{
public
String
name
;
public
Address
address
;
public
int
age
;
}
public
class
Address
{
public
String
city
,
street
;
public
int
number
,
zip
;
}
<
person
>
<
name
>
Pat
Niemeyer
</
name
>
<
address
>
<
city
>
St
.
Louis
</
city
>
<
street
>
Java
St
.</
street
>
<
number
>
1234
</
number
>
<
zip
>
54321
</
zip
>
</
address
>
<
age
>
42
</
age
>
</
person
>
But before we go there, let’s take a step back and talk about the motivation and “rules” of XML documents and some of the ways we can parse and generate them.
Get Learning Java, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.