Java 7 for Absolute Beginners

(nextflipdebug5) #1
CHAPTER 9 ■ WRITING AND READING XML

with a right angle character (>). Elements can also be empty, in which case they can take one of two
forms: a beginning tag and an ending tag with nothing between them, or a special empty element tag.
For example, an empty line element can be represented as either or . That second
structure provides a handy shortcut that saves some typing. If the poem included a blank line (such as a
line between stanzas in a longer poem), you could represent a blank line that way.


■ Note XML is case-sensitive. , , and are all different elements, so would


cause an XML parser to throw an error.


A poem offers an example of a fairly traditional document encoded as XML. Consider an example of
data transmitted between systems as XML.


Listing 9-2. XML As Data


<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">




78701



Look closely at the document specifier. In this case, it doesn't indicate encoding. The default
encoding for XML is UTF-8, so you can omit the specifier when you are going to use UTF-8 characters.
Then you have a root element named soap:Envelope. The specifier and the root element constitute the
minimum content for an XML file, but that wouldn't make a very useful message between systems. So
this example also contains a bit of data (the zip code for a particular city).This XML document represents
a request for data from one system to another system. (I invented it from scratch, by the way; NOAA may
use something else entirely.) In particular, it's a request to get the temperature in Austin, TX. Note the
indentation. When XML is meant to be read by humans, it's customarily indented, such that each level
of elements is farther to the right than its parent level. When creating a stream to send to another
system, all the white space between elements is generally removed, resulting in the whole document
being on one line. That's tough to read for a human, but it saves bandwidth, and a computer doesn't find
it hard to read.


■ Note “Document” is the normal way to refer to any instance of XML, whether it encodes an actual document


or some arbitrary bit of data—XML originally comes from the publishing industry, and it retains some of that


industry's terminology.

Free download pdf