Chapter 29 - XML


Chapter Goals



Chapter 29 - XML


29.1.1 Advantages of XML


29.1.1 Advantages of XML


29.1.1 Advantages of XML (cont.)


Quality Tip 29.1


XML is Stricter Than HTML


29.2.1 The Structure of an XML Document


29.2.1 The Structure of an XML Document (cont.)


29.2.1 The Structure of an XML Document (cont.)


29.2 Parsing XML Documents


29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents (cont.)

<?xml version="1.0"?>
<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>

29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents (cont.)


29.2 Parsing XML Documents - Parser example


29.2 Parsing XML Documents (parser.cpp)


Productivity Hint 29.1


Helper Functions in an XML Parser


Productivity Hint 29.1 (cont.)


ClassForElement getElementName(DOMNode* e)
{
   DOMNodeList* children = e->getChildNodes();
   for (int i = 0; i < children->getLength(); i++)
   {
      DOMNode* child_node = children->item(i);
      DOMElement* child_element
         = dynamic_cast<DOMElement*>(child_node);
      if (child_element != NULL)
      {
         Get value of child element
      }
   }
   Use the child element values to construct and
   return a ClassForElement object
}

Productivity Hint 29.1 (cont.)



29.3 Creating XML Documents


29.3 Creating XML Documents (cont.)


29.3 Creating XML Documents (cont.)


29.3 Creating XML Documents (cont.)


29.3 Creating XML Documents (cont.)


29.3 Creating XML Documents (cont.)


29.3 Creating XML Documents (cont.)


29.3 Creating XML Documents (cont.)


29.3 Creating XML Documents (cont.)


29.3 Creating XML Documents (cont.)


29.3 Creating XML Documents (cont.)


29.3 Creating XML Documents - Sample


29.3 Creating XML Documents (builder.cpp)


29.4 Document Type Definitions


29.4 Document Type Definitions (cont.)


29.4 Document Type Definitions (cont.)


29.4 Document Type Definitions (cont.)


29.4 Document Type Definitions (cont.)


29.4 Document Type Definitions (cont.)


29.4 Document Type Definitions (cont.)


29.4 Document Type Definitions (cont.)


29.4 Document Type Definitions (cont.)


29.4 Document Type Definitions (cont.)


29.4 Document Type Definitions (cont.)


Advanced Topic 29.1


The XML Schema Specification


Advanced Topic 29.1



Advanced Topic 29.1 (cont.)



Advanced Topic 29.1 (cont.)



29.5 Parsing with DTDs


29.5 Parsing with DTDs (cont.)


29.5 Parsing with DTDs (cont.)


29.5 Parsing with DTDs (cont.)


29.5 Parsing with DTDs (cont.)


29.5 Parsing with DTDs (cont.)


29.5 Parsing with DTDs (cont.)


29.5 Parsing with DTDs (cont.)


29.5 Parsing with DTDs (parser2.cpp)


Chapter Summary


  1. XML allows you to encode complex data, independent from any programming language, in a form that a recipient can parse easily, and that is resilient to change
  2. An XML data set is called a document. It starts out with a header and contains elements and text. An element can contain text, subelements, or both (mixed content). For data descriptions, avoid mixed content. Elements can have attributes. Use attributes to describe how to interpret the element content
  3. A parser is a program that reads a document, checks whether it is syntactically correct, and takes some action as it processes the document. There are two kinds of XML parsers. SAX fires events as it analyzes a document. DOM builds a document tree

Chapter Summary


  1. A DOMDocument object consists of nodes that are represented by objects of the DOMNode class or one of its derived classes. The most important derived classes are DOMElement and DOMText
  2. A DTD is a sequence of rules that describe the legal child elements and attributes for each element type. An XML document can contain its DTD or refer to a DTD that is stored elsewhere. When referencing an external DTD, you must supply a file name or URL
  3. It is best to use DTDs with XML files. When using DTDs, you should tell the parser to validate the document and to ignore white space. This greatly simplifies the implementation of the parser functions