From 4a2834255dc48166afc537e9e9dce80be457fa14 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 30 Sep 2009 19:14:56 +0200 Subject: New example showing handling of mixed content with type customization --- examples/cxx/tree/custom/mixed/driver.cxx | 124 ++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 examples/cxx/tree/custom/mixed/driver.cxx (limited to 'examples/cxx/tree/custom/mixed/driver.cxx') diff --git a/examples/cxx/tree/custom/mixed/driver.cxx b/examples/cxx/tree/custom/mixed/driver.cxx new file mode 100644 index 0000000..a6dd0a7 --- /dev/null +++ b/examples/cxx/tree/custom/mixed/driver.cxx @@ -0,0 +1,124 @@ +// file : examples/cxx/tree/custom/mixed/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#include +#include + +#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 + +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::auto_ptr 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 (n->getTextContent ()); + break; + } + case DOMNode::ELEMENT_NODE: + { + xhtml2txt (static_cast (n)); + break; + } + default: + break; // Ignore all other nodes (e.g., comments, etc). + } + } +} -- cgit v1.1