Read an XML Document
Before you can do much with an XML document, you need to understand its basic parts. This hack explores the most common struchture found in XML
This hack lays the basic groundwork for XML: what it looks like and how it’s put together. Example 1-1 shows a simple document (start.xml) that contains some of the most common XML structures: an XML declaration, a comment, elements, attributes, an empty element, and a character reference. start.xml is well-formed, meaning that it conforms to the syntax rules in the XML specification. XML documents must be well-formed.
Example 1-1. start.xml
1. <?xml version="1.0" encoding="UTF-8"?> 2. 3. <!-- a time instant --> 4. <time timezone="PST"> 5. <hour>11</hour> 6. <minute>59</minute> 7. <second>59</second> 8. <meridiem>p.m.</meridiem> 9. <atomic signal="true" symbol="◑"/> 10. </time>
The XML Declaration
The first line of the example contains an XML declaration, which is recommended by the XML spec but is not mandatory. If present, it must appear on the first line of the document. It is a human- and machine-readable flag that states a few facts about the content of the document.
Tip
An XML declaration is not a processing instruction, although it looks like one. Processing instructions are discussed in [Hack #3] .
In general, an XML declaration provides three pieces of information about the document that contains it: the XML version information; the character encoding in use; and whether the document stands alone or relies ...