[xsd-users] Add element that read from file to C++/Tree
sun-rise
sun.rise.ma at gmail.com
Wed Sep 30 02:11:04 EDT 2009
Hi,
I want to add a element that read from a file to C++/Tree object model.
I tried a simple case.
I was created a following schema and generated cxx/hxx code.
person.xsd
------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:pe="http://my.sample.shcema.com/person"
targetNamespace="http://my.sample.shcema.com/person">
<xsd:element name="person">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="fname" type="xsd:string" minOccurs="1" />
<xsd:element name="lname" type="xsd:string" minOccurs="1" />
<xsd:element name="age" type="xsd:int" minOccurs="1" />
<xsd:element name="desc" type="pe:desc" minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="desc" mixed="true">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:any namespace="http://www.w3.org/1999/xhtml"
processContents="skip" />
</xsd:choice>
</xsd:complexType>
</xsd:schema>
------------------------------------------------------------
xsd cxx-tree --generate-wildcard --root-element-all
--generate-serialization person.xsd
And, I was created and serialize an object model as shown in the following code.
------------------------------------------------------------
#include <iostream>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/DOMException.hpp>
#include "person.hxx"
using namespace std;
using namespace xercesc;
DOMDocument* read_xml_file(std::string file_path) {
try {
XMLPlatformUtils::Initialize();
} catch (const XMLException& toCatch) {
char * message = XMLString::transcode(toCatch.getMessage());
cout << "Error during initialization! :\n" << message << endl;
XMLString::release(&message);
return false;
}
XercesDOMParser* parser = new XercesDOMParser();
ErrorHandler* err_handler = (ErrorHandler*) new HandlerBase();
parser->setErrorHandler(err_handler);
parser->setDoNamespaces(true);
DOMDocument* doc;
try {
parser->parse(file_path.c_str());
doc = parser->getDocument();
} catch(const xercesc::XMLException& xml_exp) {
char* message = XMLString::transcode(xml_exp.getMessage());
cout << "Exception message is: \n" << message << endl;
XMLString::release(&message);
return false;
} catch(const xercesc::DOMException& dom_exp) {
char* message = XMLString::transcode(dom_exp.getMessage());
cout << "Exception message is: \n" << message << endl;
XMLString::release(&message);
return false;
} catch (...) {
cout << "Unexpected Exception" << endl;
return false;
}
return doc;
}
int main (int argc, char* argv[]) {
person::person* pe = new person::person("foo", "bar", 20);
DOMDocument* doc = read_xml_file("desc.xml");
person::person::desc_sequence& desc_seq(pe->desc());
person::person::desc_type* desc_type = new
person::desc(*(doc->getDocumentElement()));
desc_seq.push_back(*desc_type);
xml_schema::namespace_infomap map;
map[""].name = "";
map[""].schema = "";
person::person_(std::cout, *pe, map);
delete pe;
return 1;
}
------------------------------------------------------------
desc.xml
------------------------------------------------------------
<desc>
This is a sample description.
</desc>
------------------------------------------------------------
I expect
------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<p1:person xmlns:p1="http://my.sample.shcema.com/person">
<fname>foo</fname>
<lname>bar</lname>
<age>20</age>
<desc>
This is a sample description.
</desc>
</p1:person>
------------------------------------------------------------
but result
------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<p1:person xmlns:p1="http://my.sample.shcema.com/person">
<fname>foo</fname>
<lname>bar</lname>
<age>20</age>
<desc/>
</p1:person>
------------------------------------------------------------
How can I do that?
Thanks.
--
Yu
More information about the xsd-users
mailing list