Chapter 26

XML


Chapter Goals

XML

Advantages of XML

Advantages of XML

Similarities between XML and HTML

Differences Between XML and HTML

Differences Between XML and HTML

Word Processing and Typesetting Systems


Word Processing and Typesetting Systems

The Structure of an XML Document

The Structure of an XML Document

The Structure of an XML Document

The Structure of an XML Document

The Structure of an XML Document

Self Check

  1. Write XML code with a student element and child elements name and id that describe you.
  2. What does your browser do when you load an XML file, such as the items.xml file that is contained in the companion code for this book?
  3. Why does HTML use the src attribute to specify the source of an image instead of <img>hamster.jpeg</img>?

Answers

  1. <student>
    <name>James Bond</name>
    <id>007</id>
    </student>
  2. Most browsers display a tree structure that indicates the nesting of the tags. Some browsers display nothing at all because they can't find any HTML tags.
  3. The text hamster.jpg is never displayed, so it should not be a part of the document. Instead, the src attribute tells the browser where to find the image that should be displayed.

Parsing XML Documents

Parsing XML Documents

JAXP

Parsing XML Documents

Parsing XML Documents

Parsing XML Documents

An XML Document

The Tree View of XML Document

Parsing XML Documents

XPath Syntax Summary

Syntax Element Purpose Example
name Matches an element item
/ Separates elements /items/item
[n] Selects a value from a set /items/item[1]
@name Matches an attribute price/@currency
* Matches anything /items/*[1]
count Counts matches count(/items/item)
name The name of a match name(/items/*[1])

Parsing XML Documents

Parsing XML Documents

Parsing XML Documents: An Example

Parsing XML Documents: An Example

File ItemListParser.java

File ItemListParserTester.java


Output
   Ink Jet Refill Kit 29.95 8 239.6
   4-port Mini Hub 19.95 4 79.8

Self Check

  1. What is the result of evaluating the XPath statement /items/item[1]/quantity in the XML document of Figure 4?
  2. Which XPath statement yields the name of the root element of any XML document?

Answers

  1. 8.
  2. name(/*[1]).

Grammars, Parsers, and Compilers


Grammars, Parsers, and Compilers


Creating XML Documents

Creating XML Documents

DOM Interfaces for XML Document Nodes


Creating XML Documents

Creating XML Documents

Creating XML Documents

Creating XML Documents

File ItemListBuilder.java

File ItemListBuilderTester.java

Output

<?xml version="1.0" encoding="UTF-8"?><items><item><product>
<description>Toaster</description><price>29.95</price></product>
<quantity>3</quantity></item><item><product><description>Hair dryer
</description><price>24.95</price></product><quantity>1</quantity>
</item></items>

Self Check

  1. Suppose you need to construct a Document object that represents an XML document other than an item list. Which methods from the ItemListBuilder class can you reuse?
  2. How would you write a document to the file output.xml?

Answers

  1. The createTextElement method is useful for creating other documents.
  2. First construct a string, as described, and then use a PrintWriter to save the string to a file.

Validating XML Documents

Document Type Definitions

Document Type Definitions

Replacements for Special Characters

Character Encoding Name
< &lt; Less than (left angle bracket)
> &gt; Greater than (right angle bracket)
& &amp; Ampersand
' &apos; Apostrophe
" &quot; Quotation mark

DTD for Item List

<!ELEMENT items (item)*>
<!ELEMENT item (product, quantity)>
<!ELEMENT product (description, price)>
<!ELEMENT quantity (#PCDATA)>
<!ELEMENT description (#PCDATA)>
<!ELEMENT price (#PCDATA)>

Regular Expressions for Element Content

Rule Description Element Content
EMPTY No children allowed
(E*) Any sequence of 0 or more elements E
(E+) Any sequence of 1 or more elements E
(E?) Optional element E (0 or 1 occurrences allowed)
(E1, E2, . . . ) Element E1, followed by E2, . . .
(E1 | E2 | . . . ) Element E1 or E2 or . . .
(#PCDATA) Text only
(#PCDATA | E1 | E2 . . . )* Any sequence of text and elements E1, E2, . . . , in any order
ANY Any children allowed

Document Type Definitions

DTD Regular Expression Operations


DTD Regular Expression Operations

Document Type Definitions

Common Attribute Types

Type Description Attribute Type
CDATA Any character data
(V1 | V2 | . . . ) One of V1, V2, . . .

Attribute Defaults

Default Declaration Explanation
#REQUIRED Attribute is required
#IMPLIED Attribute is optional
V Default attribute, to be used if attribute is not specified
#FIXED V Attribute must either be unspecified or contain this value

Document Type Definitions

Specifying a DTD in an XML Document

Example: An Item List

<?xml version="1.0"?>
<!DOCTYPE items [

<!ELEMENT items (item*)>
<!ELEMENT item (product, quantity)>
<!ELEMENT product (description, price)>
<!ELEMENT quantity (#PCDATA)>
<!ELEMENT description (#PCDATA)>
<!ELEMENT price (#PCDATA)>

]>
<items>
   <item>
      <product>
         <description>Ink Jet Refill Kit</description>
         <price>29.95</price>
      </product>
      <quantity>8</quantity>
   </item>
   <item>
      <product>
         <description>4-port Mini Hub</description>
         <price>19.95</price>
      </product>
      <quantity>4</quantity>
   </item>
</items>

Specifying a DTD in an XML Document

Parsing and Validation

Parsing with Document Type Definitions

Self Check

  1. How can a DTD specify that the quantity element in an item is optional?
  2. How can a DTD specify that a product element can contain a description and a price element, in any order?
  3. How can a DTD specify that the description element has an optional attribute language?

Answers

  1. <!ELEMENT item (product, quantity?)>
  2. <!ELEMENT product ((description, price) | (price, description))>
  3. <!ATTLIST description language CDATA #IMPLIED>