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

(singke) #1
ptg7068951

302 HOUR 21: Reading and Writing XML Data


An XMLelement also can have one ormore attributes, which are defined
inside its start tag as name=”value”pairs. Attributes must be separated by
spaces. They provide supplemental information about the element.
Each entryelement has an attribute and a value:
<entrykey=”showEmail”>no</entry>

This element has the value “no” and a keyattribute with the value
“showEmail”.
One kind of XMLelement isn’t present in Listing 21.2: an element defined
entirely as a single tag. These elements begin with the <character, followed
by the element name and the />characters.
For instance, this element could be present as a child of the properties
element:
<inactive />

Although XMLhas been described as a format and compared to HTML,
it’s not actually a language itself. Instead, XMLdescribes how to create
data formats specific to the tasks you want to accomplish with a computer
program. XMLformats are called dialects.
The XMLdialect created by Java’s Propertiesclass is an example of this.
Oracle has developed this format for the representation of software config-
uration settings.
Data that follows the rules of XMLformatting is described as well-formed.
Software that reads or writes XMLmust accept well-formed data.
Data also can follow a more meticulous standard called validity. A valid
XMLfile contains the right elements in the rightplaces, requiring some
means of defining the valid elements.

Reading an XML File
As you have discovered during thefirst 20 hours, 13 minutes, and 52 sec-
onds of this book, a wealth of Java code is already written for you to great-
ly simplify your job. Within the Java class library, you can adopt Swing
classes for user interface programming, the java.ioclasses for file access,
java.awt.eventto take user input, and other classes to do as little pro-
gramming of your own as possible.
Free download pdf