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

(singke) #1
ptg7068951

Creating an XML File 301

When you run the application, it creates the XMLfile properties.xml,
which is presented in Listing 21.2.


LISTING 21.2 The Full Text of properties.xml
1: <?xmlversion=”1.0”encoding=”UTF-8”?>
2: <!DOCTYPEpropertiesSYSTEM“http://java.sun.com/dtd/properties.dtd”>
3:
4: Created on Wed Jun 15 20:56:33 EDT 2011
5: <entrykey=”showEmail”>no
6: <entrykey=”browser”>Mozilla Firefox
7: <entrykey=”username”>rcade
8:


XMLorganizes data in a self-documenting manner, making it possible to
understand a great deal about the data simply by looking at it.


As you glance over Listing 21.2, you can tell pretty quickly how it stored
the configuration properties. The ?xmland !DOCTYPEtags might be tough
to follow, but the rest of the file should be reasonably simple.


Data in an XMLfile is surrounded by tags that look a lot like HTML, the
markup language employed on the Web.


Start tags begin with a <character followed by the name of an element and
a >character, such as on Line 3 of Listing 21.2.


End tags begin with
characters, such as on Line 8.


Everything nested within a start tag andend tag is considered to be the
element’s value.


XMLdata must have a single root element that encloses all its data. In
Listing 21.2, the root is the propertieselement defined in Lines 3–8.


An element might contain text, a child element, or multiple child elements.
The propertieselement holds four children: a commentelement and three
entryelements.


Here’s the commentelement:


Created on Wed Jun 15 20:56:33 EDT 2011

This element has the value of the text it encloses: “Created on Wed Jun 15
20:56:33 EDT 2011.”

Free download pdf