This is a quick introduction to the mapping of the W3C XML Schema constructs to the C++ programming language. This mapping represents information stored in XML instance documents as a tree-like in-memory data structure.

1. Create a Schema definition for instance documents

  Suppose you have the following instance document (`hello.xml`):

      <?xml version="1.0"?>
      <hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="hello.xsd">

        <greeting>Hello</greeting>

        <name>sun</name>
        <name>earth</name>
        <name>world</name>

      </hello>

  Then, the Schema definition for the above instance could look like this (`hello.xsd`):

      <?xml version="1.0"?>
      <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

        <xsd:complexType name="hello">
          <xsd:sequence>
            <xsd:element name="greeting" type="xsd:string"/>
            <xsd:element name="name" type="xsd:string" maxOccurs="unbounded"/>
          </xsd:sequence>
        </xsd:complexType>

        <xsd:element name="hello" type="hello"/>

      </xsd:schema>

2. Translate your Schema to C++

      $ xsd cxx-tree hello.xsd

  Now we have `hello.hxx` and `hello.cxx`. The following listing gives an idea about the correspondence between W3C XML Schema and C++.

3.Implement application logic

  At this point we have all the tools to do something useful with the information stored in instance documents (`driver.cxx`):

      #include "hello.hxx"

      int
      main (int argc, char* argv[])
      {
        auto_ptr<class hello> h (hello (argv[1]));

        for (hello::name::const_iterator i (h->name ().begin ());
             i != h->name ().end ();
             ++i)
        {
          cerr << h->greeting () << ", " << *i << "!" << endl;
        }
      }

4. Compile and run

      $ g++ -o driver driver.cxx hello.cxx -lxerces-c
      $ ./driver hello.xml
      Hello, sun!
      Hello, moon!
      Hello, world!