Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

WHAT YOU’LL LEARN IN
THIS HOUR:
.Reading XML from a file
.Extracting XML elements
.Collecting a set of child
elements
.Reading attribute values
for an XML element
.Writing an XML file

The rise to prominence of Java in the 1990s coincided with another dramat-
ic change in the development of computer software: the introduction of
Extensible Markup Language (XML). XML, a format for organizing and
storing data so that it can be read by any program, has become ginormous,
to borrow my kids’ favorite adjective.


Thanks to XML, data can be read and written independently of the soft-
ware used to create it. This is a welcome change from the bad old days
when every program seemed to have its own proprietary and idiosyncratic
format.


XMLdata can be read with a parser, a program that recognizes the format
and can extract portions of the data as needed.


During this hour, you read and write XMLdata using the XMLObject
Model(XOM), a Java class library that makes it easy to work with XML
data in Java programs.


Creating an XML File


Before exploring XOM, you should learn some things about XMLand how it
stores data. XMLdata turns up in countless places—it can be stored to a file,
transmitted over an Internet network, and held in a program’s memory.


Several classes in the Java class library can read and write XML, including
the Propertiesclass in the java.utilpackage, which was covered in
Hour 20, “Reading and Writing Files.”


APropertiesobject can be stored as XMLrather than in the name=value
format covered in the preceding hour.


HOUR 21


Reading and Writing XML Data

Free download pdf