summaryrefslogtreecommitdiff
path: root/xsd-examples/cxx/tree/custom/mixed/driver.cxx
blob: 08c83e087f7784541c45d12dc0b2a4ad35a647c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// file      : cxx/tree/custom/mixed/driver.cxx
// copyright : not copyrighted - public domain

#include <memory>   // std::unique_ptr
#include <iostream>

#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/PlatformUtils.hpp>

#include "people.hxx"

// The following transcode() utility function is handy when working with
// Xerces. Include it after the generated header in order to get only char
// or wchar_t version depending on how you compiled your schemas.
//
#include <xsd/cxx/xml/string.hxx>

using std::cerr;
using std::endl;
using namespace xercesc;

void
xhtml2txt (const DOMElement*);

int
main (int argc, char* argv[])
{
  if (argc != 2)
  {
    cerr << "usage: " << argv[0] << " people.xml" << endl;
    return 1;
  }

  int r (0);

  // The Xerces-C++ DOM document that will be used to store the XHTML
  // fragments "out-live" the call to the parsing function. Therefore
  // we need to initialize the Xerces-C++ runtime ourselves.
  //
  XMLPlatformUtils::Initialize ();

  try
  {
    using namespace people;

    // Parse.
    //
    std::unique_ptr<directory> d (
      directory_ (argv[1], xml_schema::flags::dont_initialize));

    // Print what we've got.
    //
    const directory::person_sequence& s (d->person ());

    for (directory::person_const_iterator i (s.begin ()); i != s.end (); ++i)
    {
      cerr << "First  : " << i->first_name () << endl
           << "Last   : " << i->last_name () << endl
           << "Gender : " << i->gender () << endl
           << "Age    : " << i->age () << endl;

      const bio& b (i->bio ());
      const DOMElement* xhtml (b.xhtml ());

      if (xhtml != 0)
      {
        cerr << "Bio    : " << endl;
        xhtml2txt (xhtml);
      }

      cerr << endl;
    }

    // Serialize.
    //
    xml_schema::namespace_infomap map;

    map["ppl"].name = "http://www.codesynthesis.com/people";
    map["ppl"].schema = "people.xsd";

    directory_ (
      std::cout, *d, map, "UTF-8", xml_schema::flags::dont_initialize);
  }
  catch (const xml_schema::exception& e)
  {
    cerr << e << endl;
    r = 1;
  }

  XMLPlatformUtils::Terminate ();
  return r;
}

// Primitive XHTML to text converter that just prints all the text
// nodes and ignores everything else.
//
void
xhtml2txt (const DOMElement* e)
{
  namespace xml = xsd::cxx::xml;

  for (const DOMNode* n (e->getFirstChild ());
       n != 0;
       n = n->getNextSibling ())
  {
    switch (n->getNodeType ())
    {
    case DOMNode::TEXT_NODE:
      {
        cerr << xml::transcode<char> (n->getTextContent ());
        break;
      }
    case DOMNode::ELEMENT_NODE:
      {
        xhtml2txt (static_cast<const DOMElement*> (n));
        break;
      }
    default:
      break; // Ignore all other nodes (e.g., comments, etc).
    }
  }
}