- Introduction
- DOM Basics
- Using the MSXML DOM from C++
- A Simple XML Document and Its Schema
- Consuming XML
- Producing XML
- Conclusion
- For More Information
A Simple XML Document and Its Schema
For both sample programs we'll use the same simple XML document and schema. To keep the code in both programs simple and focusing on basic DOM operations, we'll use a very generic nonsense document, but we'll make sure that it has the basic features we need. Here's the document:
<?xml version="1.0" encoding="UTF-8"?> <SampleDocumentElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="SampleSchema.xsd"> <!--This comment is a child of the document element--> <FirstLevelChild Attribute1="Attribute Value"> <SecondLevelChild>Element Text</SecondLevelChild> </FirstLevelChild> <FirstLevelChild/> </SampleDocumentElement>
Notice that we have the XML declaration, the document element SampleDocumentElement, a comment, and some child elements after it. The first child element has an attribute and a child element with text. The schema for this simple document is shown next:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified"> <xs:element name="SampleDocumentElement"> <xs:complexType> <xs:sequence> <xs:element name="FirstLevelChild" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="SecondLevelChild" type="xs:string" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="Attribute1" type="xs:string"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
The specific coding style of the schema is mostly irrelevant to this article and even to the DOM, but I include it here so that you can have a complete example.
Both of our sample programs are coded as Win32 console applications. To keep them concise, they're fully self-contained with no header files or external routines. Because consuming XML tends to be a bit simpler than producing it (especially when MXSML and schemas are concerned), we'll start with consuming.