Prerequisites
xmlbeansxx is similar to Java XmlBeans. It is recommended to know XmlBeans before reading this document (and also boost library shared pointers, boost::shared_ptr, boost::shared_array, etc.).
Suppose we have schema documents named schema.xsd, schema2.xsd. We assume that schema.xsd includes schema2.xsd, and that schema.xsd defines document element named root.
Generate classes
First, we have to install xmlbeansxx-gen and add xmlbeansxx-gen/dist/bin directory to execution path. Then we install xmlbeansxx library.
Now we are ready to generate classes. We do this by command:
scompxx schema.xsd schema2.xsd
This script creates in current directory files schema.h, schema.cpp, schema2.h, schema2.cpp, containing generated classes from types defined in schemas. Each given file.xsd is compiled into file.h and file.cpp. Schema namespaces are translated into C++ namespaces, just like Java XmlBeans translates them into package names.
Let's use them
Now we are ready to parse, create and validate documents.
Parsing and creating documents without validation.
In files schema.h and schema2.h there are defined classes, which derive from XmlObject. They have static methods parse() and fromString(), which can be used to parse document. Also there are virtual methods serialize() and toString() on XmlObject, which can be used to serialize. To see examples, look at directory examples/simple in xmlbeansxx.
Parsing with validation.
Validation in XmlBeans++ is performed using xerces-c schema validator. To do this, we create XmlParser object. Then, using XmlParser::loadGrammar() function, we load our grammars (we can specify only schema.xsd file, because schema2.xsd will be loaded automatically by xerces-c). After this, we create RootDocument object and use XmlParser::parse() method giving it our RootDocument object. If there are errors, an exception of class BeansException is thrown.
Validation is possible only on document types.
Validating a document.
It can be done by serializing document type to string and parsing it with validation, as in 2.
Simple Types.
We can get to simple content of element using XmlObject::getSimpleContent() virtual method. (Note that XmlObject::toString() virtual method used on simple type object will return string containing xml document, not only content).
There are implemented some builtin simple types, like XmlInteger, XmlDecimal, XmlString, XmlBoolean with specific operations.
Add Comment