Java Reference
In-Depth Information
CHAPTER 19
XML
XML, or Extensible Markup Language, is a meta-language for marking up text doc-
uments with structural tags, similar to those found in HTML and SGML documents.
XML has become popular because its structural markup allows documents to
describe their own format and contents. XML enables “portable data,” and it can
be quite powerful when combined with the “portable code” enabled by Java.
Because of the popularity of XML, there are a number of tools for parsing and
manipulating XML documents. And because XML documents are becoming more
and more common, it is worth your time to learn how to use some of those tools
to work with XML. The examples in this chapter introduce you to simple XML
parsing and manipulation. If you are familiar with the basic structure of an XML
file, you should have no problem understanding them. Note that there are many
subtleties to working with XML; this chapter doesn't attempt to explain them all.
To learn more about XML, try Java and XML , by Brett McLaughlin, or XML Pocket
Refer ence , by Robert Eckstein, both from O'Reilly & Associates.
The world of XML and its affiliated technologies is moving so fast that it can be
hard just keeping up with the acronyms, standards, APIs, and version numbers. I'll
try to provide an overview of the state of various technologies in this chapter, but
be warned that things may have changed, sometimes radically, by the time you
read this material.
Parsing with JAXP and SAX 1
The first thing you want to do with an XML document is parse it. There are two
commonly used approaches to XML parsing: they go by the acronyms SAX and
DOM. We'll begin with SAX parsing; DOM parsing is covered later in the chapter.
At the very end of the chapter, we'll also see a new, but very promising, Java-cen-
tric XML API known as JDOM.
SAX is the Simple API for XML. SAX is not a parser, but rather a Java API that
describes how a parser operates. When parsing an XML document using the SAX
API, you define a class that implements various “event” handling methods. As the
Search WWH ::




Custom Search