XmlBeansxx / XmlBeanscxx Description
The XmlBeansxx program and process is a C++ code generation program driven by an XSD XML schema definition.
XmlBeansxx can read an XSD schema definition and auto-generate C++ classes to parse and access an XML file which meets the schema definition.
The very same classes can also be used to generate XML conforming to the XSD schema.
XmlBeansxx will generate C++ classes and provide a library which can be compiled in or linked into your C++ program.
There are functions in these classes which can be used to obtain values from a parsed XML file or to generate an XML file.
XML validation is optional.
XML marshalling and unmarshalling utilizing XML Beans generated C++ classes
XmlBeansxx Installation:
Prerequisites:
- autoconf and automake (or cmake)
- RHEL5 (autoconf: 2.59, automake: 1.9.6): yum install autoconf automake
- Ubuntu 8.10 (autoconf: 2.61, automake: 1.10): apt-get install autoconf automake
- Ubuntu 12.04 (autoconf: 2.68, automake: 1.11.3): apt-get install autoconf automake
- libtool:
- RHEL5 (libtool: 1.5.22): yum install libtool
(So old it leads to problems! See pitfalls below)
- Ubuntu 8.10 (libtool: 2.2.4): apt-get install libtool
- Ubuntu 12.04 (libtool: 2.4.2): apt-get install libtool
- gmp (http://gmplib.org/)
- RHEL5 (gmp 4.1.4): yum install gmp gmp-devel
- Ubuntu 8.10 (libgmpxx4 4.2.2, libgmp3-dev: 4.2.2): sudo apt-get install libgmpxx4 libgmp3-dev
- Ubuntu 12.04 (libgmpxx4ldbl 5.0.2, libgmp3-dev: 5.0.2): sudo apt-get install libgmpxx4ldbl libgmp3-dev
- boost and boost-devel
- RHEL6 (boost: 1.41.0): yum install boost boost-devel boost-test boost-date-time boost-thread boost-system boost-signals boost-filesystem boost-regex boost-program-options boost-wave boost-graph boost-python boost-serialization boost-iostreams
- RHEL5 (boost: 1.33.1): yum install boost boost-devel
- Ubuntu 8.10 (libboost-dev: 1.34.1): sudo apt-get install libboost-dev
- Ubuntu 12.04 (libboost-dev: 1.48.0.2): sudo apt-get install libboost-date-time1.48.0
sudo ln -s /usr/lib/libboost_date_time.so.1.48.0 /usr/lib/libboost_date_time.so
sudo apt-get install libboost-all-dev
sudo ldconfig (this registers the library to the linker environment)
- xerces-c: See the YoLInux.com Xerces-C 2.7.0 tutorial (Note that XmlBeansxx 0.9.8 works with Xerces-C 2.7.0 (and even 2.8) but not with Xerces-C 3.0.1 or anything later than 2.8)
- RHEL5: Download source, compile and generate RPMs for installation: rpmbuild -ta xerces-c-2.7.0.tar.gz
(Use RPMs as they install to /usr - required as XmlBeansxx does not allow one to specify an alternate path for Xerces)
Also available for RHEL5 as an RPM from EPEL.
- Ubuntu: Compile from source and install to /usr
[Potential Pitfall]: If you get the following error: XercesParser.cpp:34:51: fatal error: xercesc/internal/XMLGrammarPoolImpl.hpp: No such file or directory
you are probably using the wrong version of xerces (paths were changed). Use Xerces-c 2.7 or 2.8.
- Install Java (1.4.0+ including 1.6): (XmlBeans code generation requires Java and the Maven build tool is a Java program)
See the YoLinux Java installation tutorial
- Install Maven (untar Java jar files. Java program and thus platform independent)
(Called by XmlBeans configure script and Makefile)
- Download compiled Java program: http://maven.apache.org/download.html
- cd /opt
- wget http://apache.opensourceresources.org/maven/binaries/apache-maven-2.2.1-bin.tar.gz
- tar xzf apache-maven-2.2.1-bin.tar.gz
This creates /opt/apache-maven-2.2.1
- Configuration:
- Add path to shell environment: export PATH=$PATH:/opt/apache-maven-2.2.1/bin
This allows execution of the Maven build command mvn
- If using a proxy, edit /opt/apache-maven-2.2.1/conf/settings.xml
Set proxy: <proxies> <proxy> ...
- Add mvn to path: export PATH=$PATH:/opt/apache-maven-2.2.1/bin
Note: Maven will require an internet connection to download the appropriate plug-ins during operation.
Ubuntu/Debian Note: Maven can also be installed using apt-get: sudo apt-get install maven2
- Log4cxx is an optional prerequisite (Version 0.9.7)
See the YoLinux.com Log4cxx tutorial for
Red Hat Enterprise Linux 5 (RHEL5) prerequisites and build instructions.
Note that XmlBeansxx requires that Log4cxx be installed to /usr and is not recognized by the XmlBeans build in any other location such as /opt or /usr/local.
Build and install:
- Build and install Log4cxx to /usr (See the Log4cxx tutorial)
- Download xmlbeansxx-0.9.8.tar.gz
- tar xzf xmlbeansxx-0.9.8.tar.gz
- cd xmlbeansxx-0.9.8
- ./bootstrap
[Potential Pitfall]: CppUnit errors
+ mkdir -p config m4
+ aclocal -I m4
configure.ac:36: warning: macro `AM_PATH_CPPUNIT' not found in library
+ autoheader
+ libtoolize --automake --copy
+ automake --foreign --add-missing --copy
tests/Makefile.am:7: HAVE_CPPUNIT does not appear in AM_CONDITIONAL
+ autoconf
Fix by installing cppunit and cppunit-devel packages (RPMs)
- ./configure --prefix=/usr --disable-log4cxx (or with log4cxx if you plan to use its' logging facility.)
Typically I would specify /opt or /usr/local but execution of xmlbeansxx scompxx command requires /usr in order to function. (As Far As I Can Tell)
[Potential Pitfall]: RHEL6.3 configure boost error:
...
checking for main in -lboost_thread... no
configure: error: boost_thread library not found. Please install it.
Check dependency: rpm -qa|grep boost-thread
boost-thread-1.41.0-11.el6_1.2.x86_64
boost-thread-1.41.0-11.el6_1.2.i686
Note that the libraries are now named:
/usr/lib/libboost_thread-mt.so.5
/usr/lib64/libboost_thread-mt.so
/usr/lib64/libboost_thread-mt.so.5
Thus edit the file xmlbeansxx-0.9.8/configure:
Edit lines 16758 and 16816 from:
LIBS="-lboost_thread $LIBS"
to:
LIBS="-lboost_thread-mt $LIBS"
- make
[Potential Pitfall]: RHEL5:
If you get the error (and you will):
../../libtool: line 466: CDPATH: command not found
../../libtool: line 1144: func_opt_split: command not found
This due to the file ./libtool which is created by ./configure is for
the newer version of libtool 2.2.4
while RHEL5 has libtool version 1.5.22 /usr/bin/libtool which does not have these functions.
Thus fix by:
- mv libtool NoGood-libtool
- ln -s /usr/bin/libtool ./
Then try make again.
Note Ubuntu 8.10 uses libtool 2.2.4 and does not have this problem.
- make check
This requires cppunit to be installed before executing "configure".
(Note: "CalendarTest" failed for me, the rest was Ok)
[Potential Pitfall]: Library error
xmlbeansxx-0.9.8/tests/.libs/lt-XXXTest: error while loading shared libraries: liblog4cxx.so.10: cannot open shared object file: No such file or directory
Fix: Register the newly generated libraries with the command (as root): ldconfig -n /usr/lib
- make install
Installs to /usr/include/xmlbeansxx/, /usr/lib, /usr/bin and /usr/share/xmlbeansxx/ for configure "--prefix=/usr".
[Potential Pitfall]: not all files installed (RHEL6)
cd src/xmlbeansxx
sudo /usr/bin/install -c -m 644 configLibxml2.h configLog4cxx.h configGmpxx.h XmlTypesGen.h xml-fragment.h '/usr/include/xmlbeansxx'
Links:
XmlBeansxx Examples:
The first example is using a variation of the Apache XML Beans sample XSD and XML
The second is our own YoLinux.com example using a more complex XSD and XmlBeansxx compiled with Log4cxx.
The third example generates XML which complies to an XSD.
Apache XML Beans XSD example: Parsing XML
The following schema example is based on the
Apache XML Beans example.
File:
EasyPO.xsd
20 | elementFormDefault = "qualified" > |
21 | < xs:element name = "purchase-order" > |
24 | < xs:element name = "customer" type = "po:customer" /> |
25 | < xs:element name = "date" type = "xs:dateTime" /> |
26 | < xs:element name = "line-item" type = "po:line-item" minOccurs = "0" maxOccurs = "unbounded" /> |
27 | < xs:element name = "shipper" type = "po:shipper" minOccurs = "0" /> |
31 | < xs:complexType name = "customer" > |
33 | < xs:element name = "name" type = "xs:string" /> |
34 | < xs:element name = "address" type = "xs:string" /> |
37 | < xs:complexType name = "line-item" > |
39 | < xs:element name = "description" type = "xs:string" /> |
40 | < xs:element name = "per-unit-ounces" type = "xs:decimal" /> |
41 | < xs:element name = "price" type = "xs:double" /> |
42 | < xs:element name = "quantity" type = "xs:int" /> |
45 | < xs:complexType name = "shipper" > |
47 | < xs:element name = "name" type = "xs:string" /> |
48 | < xs:element name = "per-ounce-rate" type = "xs:decimal" /> |
Note:
- The "xs:" prefix refers to XML built-in type definitions defined by the w3.org (World Wide Web consortium).
- The "po:" prefix refers to "Purchase Order" schema defined by the enumerations in http://xmlbeans.apache.org/samples/enumeration/schemaenum/easypo
Generate C++ classes from XSD: /usr/bin/scompxx EasyPO.xsd
[Potential Pitfall]:
scompxx on RHEL6 runtime error:
libtoolize: `COPYING.LIB' not found in `/usr/share/libtool/libltdl'
The scompxx command is a bash shell script which performs many operations including the generation and execution of a configure script.
This error can be fixed by installing two packages not included with the RHEL6.3 workstation DVD but can be found on pkgs.org as CentOS RPM packages:
- libtool-ltdl-devel-2.2.6-15.5.el6.x86_64.rpm
- libtool-ltdl-devel-2.2.6-15.5.el6.i686.rpm
File:
testEasyPO.cpp
06 | using namespace xmlbeansxx; |
07 | using namespace xmlbeansxx::samples::enumeration::schemaenum::easypo; |
12 | fstream in( "easypo.xml" , ios::in); |
13 | PurchaseOrderDocument pDoc(PurchaseOrderDocument::Factory::parse(in)); |
15 | xmlbeansxx::XmlOptions opts; |
16 | opts.setValidation( true ); |
18 | vector<LineItem> arr = pDoc.getPurchaseOrder().getLineItemArray(); |
19 | for ( int i=0; i < arr.size() ; i++) { |
20 | cout << "item: " << i << endl; |
21 | cout << " - description: " << arr[i].getDescription() << endl; |
22 | cout << " - quantity: " << arr[i].getQuantity() << endl; |
23 | cout << " - price: " << arr[i].getPrice() << endl; |
24 | cout << " - amount: " << arr[i].getQuantity() * arr[i].getPrice() << endl; |
29 | } catch (BeansException &ex) { |
30 | cout << "BeansException: " << ex.getMessage() << endl; |
Compile and link:
g++ -g -o test testEasyPO.cpp src/EasyPO.cpp -Isrc/ -lxerces-c -lgmp -lgmpxx -pthread -lxmlbeansxx /usr/lib/libxmlbeansxx.a
(Static link)
[Potential Pitfall]: RHEL6.3
Compiling errors due to missing include files.
For some unknown reason some installations are incomplete and do not copy all of the required include files to
/usr/include/xmlbeansxx/ (in my case five were missing).
This will then have to be performed manually:
cd xmlbeansxx-0.9.8/src/xmlbeansxx
sudo /usr/bin/install -c -m 644 configLibxml2.h configLog4cxx.h configGmpxx.h XmlTypesGen.h xml-fragment.h '/usr/include/xmlbeansxx'
File:
easypo.xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
06 | < name >Larry Kowalski</ name > |
07 | < address >ul. Anytown, PA</ address > |
09 | < date >2005-01-01T12:00:00</ date > |
11 | < description >Burnham's Celestial Handbook Vol 1</ description > |
12 | < per-unit-ounces >10</ per-unit-ounces > |
14 | < quantity >1</ quantity > |
17 | < description >Burnham's Celestial Handbook Vol 2</ description > |
18 | < per-unit-ounces >7</ per-unit-ounces > |
20 | < quantity >1</ quantity > |
23 | < description >Introduction to Linux</ description > |
24 | < per-unit-ounces >7</ per-unit-ounces > |
26 | < quantity >1</ quantity > |
30 | < per-ounce-rate >0.65</ per-ounce-rate > |
Test by running the resulting program:
./test
Locale: en_US.UTF-8
item: 0
- description: Burnham's Celestial Handbook Vol 1
- quantity: 1
- price: 23
- amount: 23
item: 1
- description: Burnham's Celestial Handbook Vol 2
- quantity: 1
- price: 22
- amount: 22
item: 2
- description: Introduction to Linux
- quantity: 1
- price: 27
- amount: 27
A More Complex XSD example: Parsing XML
Sample XSD schema to define a corporation, employees and their data.
Generate C++ classes from XSD: /usr/bin/scompxx corporation.xsd
This will generate the files src/corporation.h and src/corporation.cpp
This is a hand cleanup script for all of the extra files you probably will not need:
rm -Rf config.h.in stamp-h1 config.log config.h Makefile config.status libtool configure
rm -Rf Makefile.in config autom4te.cache aclocal.m4 bootstrap configure.ac m4 Makefile.am
rm src/*.o src/xmltypes src/xsd.txt src/Makefile.am src/Makefile.in src/Makefile
rm -Rf src/*.lo src/libxmltypes.la src/xmltypes_headers.h src/.deps src/.libs
File:
testProgramMain.cpp
01 | #include "corporation.h" |
06 | #include <log4cxx/logger.h> |
07 | #include <log4cxx/xml/domconfigurator.h> |
10 | using namespace xmlbeansxx; |
11 | using namespace log4cxx; |
12 | using namespace log4cxx::xml; |
13 | using namespace log4cxx::helpers; |
15 | LoggerPtr testProgramMain(log4cxx::Logger::getLogger( "TestProgramMain" )); |
19 | DOMConfigurator::configure( "Log4cxxConfig.xml" ); |
22 | fstream xmlData( "CorporationMegaX.xml" , ios::in); |
24 | noNamespace::RootDocument_Root *pDoc = new noNamespace::RootDocument_Root(noNamespace::RootDocument_Root::Factory::parse(xmlData)); |
26 | cout << "Corporation Name: " << pDoc->getCorporation().getName() << endl; |
27 | cout << "Corporation Address: " << pDoc->getCorporation().getAddress() << endl << endl; |
30 | vector<noNamespace::RootDocument_Root_People_Employee> arr = pDoc->getPeople().getEmployeeArray(); |
34 | for ( int i=0; i < arr.size() ; i++) |
36 | cout << "Employee name: " << arr[i].getName() << endl; |
37 | switch (arr[i].getTaxStatus()) |
40 | cout << " Employee number: " << arr[i].getUSW2().getEmpNumber() << endl; |
43 | cout << " Employee SSN number: " << arr[i].getUS1099().getSsnNumber() << endl; |
49 | cout << " Work phone: " << arr[i].getData().getWorkPhone() << endl; |
54 | } catch (BeansException &ex) { |
55 | cout << "BeansException: " << ex.getMessage() << endl; |
Compile and link: g++ -g -o test testProgramMain.cpp src/corporation.cpp -Isrc/ -I/opt/include -lxerces-c -lgmp -lgmpxx -pthread -lxmlbeansxx /usr/lib/libxmlbeansxx.a /usr/lib/liblog4cxx.a -lapr-1 -laprutil-1
(Static link)
File:
CorporationMegaX.xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
04 | < Name >Corporation MegaX</ Name > |
05 | < Phone >1-800-555-1212</ Phone > |
06 | < Fax >1-877-555-1212</ Fax > |
07 | < Address >512 Megacorp Way, Gotham City</ Address > |
11 | < Employee TaxStatus = "US-W2" Gender = "Male" Desc = "OnSite" > |
12 | < Name >Mr Grand Kahuna</ Name > |
14 | < EmpNumber >1</ EmpNumber > |
15 | < Manager >None</ Manager > |
16 | < YearStart >1998</ YearStart > |
19 | < WorkPhone >1-800-555-1213</ WorkPhone > |
20 | < CellPhone >1-800-555-1214</ CellPhone > |
21 | < Address >100 Cherry Hill Lane, Gotham City</ Address > |
25 | < Employee TaxStatus = "US-1099" Gender = "Male" Desc = "OnSite" > |
26 | < Name >Mr Special Tee</ Name > |
28 | < SsnNumber >123-45-6788</ SsnNumber > |
29 | < Phone >1-817-555-1212</ Phone > |
30 | < CorpName >ABC Consulting</ CorpName > |
31 | < CorpAddress >3 Mockingbird Lane, Smallville AK</ CorpAddress > |
32 | < Relationship >CorpToIndividual</ Relationship > |
35 | < WorkPhone >1-800-555-1215</ WorkPhone > |
36 | < CellPhone >1-800-555-1216</ CellPhone > |
37 | < Address >200 Lookout Hill, Gotham City</ Address > |
41 | < Employee TaxStatus = "US-W2" Gender = "Female" Desc = "OnSite" > |
42 | < Name >Mrs Jenny Reliable</ Name > |
44 | < EmpNumber >2</ EmpNumber > |
45 | < Manager >Mr Grand Kahuna</ Manager > |
46 | < YearStart >1999</ YearStart > |
49 | < WorkPhone >1-800-555-1217</ WorkPhone > |
50 | < CellPhone >1-800-555-1218</ CellPhone > |
51 | < Address >300 Riverside View, Gotham City</ Address > |
Test by running the resulting program:
./test
Locale: en_US.UTF-8
Corporation Name: Corporation MegaX
Corporation Address: 512 Megacorp Way, Gotham City
Employee name: Mr Grand Kahuna
Employee number: 1
Work phone: 1-800-555-1213
Employee name: Mr Special Tee
Employee SSN number: 123-45-6788
Work phone: 1-800-555-1215
Employee name: Mrs Jenny Reliable
Employee number: 2
Work phone: 1-800-555-1217
File (minimum) for Log4cxx:
Log4cxxConfig.xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
05 | < appender name = "testConsoleAppender" class = "org.apache.log4j.ConsoleAppender" > |
06 | < param name = "Target" value = "System.out" /> |
07 | < layout class = "org.apache.log4j.PatternLayout" > |
08 | < param name = "ConversionPattern" value = "%-5p %c{1} - %m%n" /> |
13 | < priority value = "error" /> |
14 | < appender-ref ref = "testConsoleAppender" /> |
19 | < category name = "TestProgramMain" > |
20 | < priority value = "all" /> |
24 | < category name = "xmlbeansxx" > |
25 | < priority value = "warn" /> |
28 | </ log4j:configuration > |
For more on Apache Log4cxx, see the YoLinux.com Log4cxx tutorial
XML Validation and Parsing:
XML validation is a capability of the parser which can be turned on with the following code snippet:
1 | xmlbeansxx::XmlOptions xml_options; |
2 | xml_options.setValidation( true ); |
4 | noNamespace::RootDocument_Root *pDoc = new noNamespace::RootDocument_Root(noNamespace::RootDocument_Root::Factory::parse(xmlData, xml_options)); |
Note that this call to "parse()" has two arguments, the second being the inclusion of "XmlOptions".
When validation is turned on and there is an XML error, you get a detailed (although not always helpful) error message:
BeansException: Xml:While parsing: Xerces-c error: At line 3, char 685, Not enough elements to match content model : '(Corporation,People)', std
In this case the required element "Corporation" was missing.
When parser validation is not turned on, you still get an error message but it is less specific:
BeansException: NUllPtr exception: Cannot get an element from a empty XmlObject: noNamespace::RootDocument_Root_Corporation
Generating XML using XML Beansxx
Generating XML using XMLBeansxx uses the same source files generated by scompx.
File:
testProgramMain.cpp
01 | #include "corporation.h" |
06 | #include <log4cxx/logger.h> |
07 | #include <log4cxx/xml/domconfigurator.h> |
10 | using namespace xmlbeansxx; |
11 | using namespace log4cxx; |
12 | using namespace log4cxx::xml; |
13 | using namespace log4cxx::helpers; |
14 | using namespace noNamespace; |
16 | LoggerPtr testProgramMain(log4cxx::Logger::getLogger( "TestProgramMain" )); |
20 | DOMConfigurator::configure( "Log4cxxConfig.xml" ); |
22 | RootDocument doc_obj = RootDocument::Factory::newInstance(); |
25 | RootDocument_Root root_obj = RootDocument_Root::Factory::newInstance(); |
26 | RootDocument_Root_People people_obj = RootDocument_Root_People::Factory::newInstance(); |
29 | RootDocument_Root_Corporation corporation_obj = RootDocument_Root_Corporation::Factory::newInstance(); |
30 | corporation_obj.setName( "Corporation MegaX" ); |
31 | corporation_obj.setPhone( "1-800-555-1212" ); |
32 | corporation_obj.setFax( "1-877-555-1212" ); |
33 | corporation_obj.setAddress( "512 Megacorp Way, Gotham City" ); |
34 | root_obj.setCorporation(corporation_obj); |
37 | RootDocument_Root_People_Employee employee_obj_1 = RootDocument_Root_People_Employee::Factory::newInstance(); |
38 | employee_obj_1.setName( "Mr Grand Kahuna" ); |
39 | employee_obj_1.setTaxStatus(ETaxStatus::Enum::INT_US_W_2); |
40 | employee_obj_1.setGender(EGender::Enum::INT_MALE); |
41 | employee_obj_1.setDesc(ELocation::Enum::INT_ON_SITE); |
43 | RootDocument_Root_People_Employee_USW2 employee_USW2_obj_1 = RootDocument_Root_People_Employee_USW2::Factory::newInstance(); |
44 | employee_USW2_obj_1.setEmpNumber(1); |
45 | employee_USW2_obj_1.setYearStart( "1998" ); |
46 | employee_USW2_obj_1.setManager( "None" ); |
47 | employee_obj_1.setUSW2( employee_USW2_obj_1 ); |
49 | RootDocument_Root_People_Employee_Data employee_data_obj_1 = RootDocument_Root_People_Employee_Data::Factory::newInstance(); |
50 | employee_data_obj_1.setWorkPhone( "1-800-555-1213" ); |
51 | employee_data_obj_1.setCellPhone( "1-800-555-1214" ); |
52 | employee_data_obj_1.setAddress( "100 Cherry Hill Lane, Gotham City" ); |
53 | employee_obj_1.setData(employee_data_obj_1); |
54 | people_obj.addEmployee( employee_obj_1 ); |
57 | RootDocument_Root_People_Employee employee_obj_2 = RootDocument_Root_People_Employee::Factory::newInstance(); |
58 | employee_obj_2.setName( "Mrs Jenny Reliable" ); |
59 | employee_obj_2.setTaxStatus(ETaxStatus::Enum::INT_US_W_2); |
60 | employee_obj_2.setGender(EGender::Enum::INT_FEMALE); |
61 | employee_obj_2.setDesc(ELocation::Enum::INT_ON_SITE); |
63 | RootDocument_Root_People_Employee_USW2 employee_USW2_obj_2 = RootDocument_Root_People_Employee_USW2::Factory::newInstance(); |
64 | employee_USW2_obj_2.setEmpNumber(2); |
65 | employee_USW2_obj_2.setYearStart( "1999" ); |
66 | employee_USW2_obj_2.setManager( "Mr Grand Kahuna" ); |
67 | employee_obj_2.setUSW2( employee_USW2_obj_2 ); |
69 | RootDocument_Root_People_Employee_Data employee_data_obj_2 = RootDocument_Root_People_Employee_Data::Factory::newInstance(); |
70 | employee_data_obj_2.setWorkPhone( "1-800-555-1217" ); |
71 | employee_data_obj_2.setCellPhone( "1-800-555-1218" ); |
72 | employee_data_obj_2.setAddress( "300 Riverside View, Gotham City" ); |
73 | employee_obj_2.setData(employee_data_obj_2); |
74 | people_obj.addEmployee( employee_obj_2 ); |
76 | root_obj.setPeople(people_obj); |
77 | doc_obj.setRoot(root_obj); |
79 | cout << "Xml:" << endl << doc_obj.toString() << endl; |
81 | } catch (BeansException &ex) { |
82 | cout << "BeansException: " << ex.getMessage() << endl; |
Compile and link:
g++ -g -o test testProgramMain.cpp src/corporation.cpp -Isrc/ -I/opt/include -lxerces-c -lgmp -lgmpxx -pthread -lxmlbeansxx /usr/lib/libxmlbeansxx.a /usr/lib/liblog4cxx.a -lapr-1 -laprutil-1
(Static link)
Run: ./test
Note that this will use the same log4cxx XML config file as the above parsing example.
Locale: en_US.utf8
Xml:
<?xml version="1.0" encoding="UTF-8"?>
<root><Corporation><Name>Corporation MegaX</Name><Phone>1-800-555-1212</Phone><Fax>1-877-555-1212</Fax><Address>512 Megacorp Way, Gotham City</Address></Corporation><People><Employee TaxStatus="US-W2" Gender="Male" Desc="OnSite"><Name>Mr Grand Kahuna</Name><US-W2><EmpNumber>1</EmpNumber><Manager>None</Manager><YearStart>1998</YearStart></US-W2><Data><WorkPhone>1-800-555-1213</WorkPhone><CellPhone>1-800-555-1214</CellPhone><Address>100 Cherry Hill Lane, Gotham City</Address></Data></Employee><Employee TaxStatus="US-W2" Gender="Female" Desc="OnSite"><Name>Mrs Jenny Reliable</Name><US-W2><EmpNumber>2</EmpNumber><Manager>Mr Grand Kahuna</Manager><YearStart>1999</YearStart></US-W2><Data><WorkPhone>1-800-555-1217</WorkPhone><CellPhone>1-800-555-1218</CellPhone><Address>300 Riverside View, Gotham City</Address></Data></Employee></People></root>
Valid XML:
Note that this XML will not validate.
This is because <root> does not reference the schema instance.
Change <root> to:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="corporation.xsd">
This can be inserted programmatically with the following code snippet:
03 | string SchemaLocationPrefix = "xsi:noNamespaceSchemaLocation=\"corporation.xsd\"" ; |
04 | string TagToModify( "<root>" ); |
05 | size_t insert_index = doc_obj.toString().find(TagToModify) + TagToModify.length() - 1; |
06 | string insXml = " " + SchemaInstance + " " + SchemaLocationPrefix; |
08 | string modXml(doc_obj.toString().substr(0,insert_index) + insXml + doc_obj.toString().substr(insert_index,doc_obj.toString().length())); |
10 | cout << modXml << endl; |
The beginning of the XML will be valid and look as follows:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="corporation.xsd"><Corporation><Name>Corporation MegaX</Name> ...
Verifying XML Validation Programmatically:
XmlBeansxx can validate the XML you have just generated by parsing it with the following code snippet:
2 | xmlbeansxx::XmlOptions xml_options; |
3 | xml_options.setValidation( true ); |
4 | RootDocument_Root::Factory::parse(modXml,xml_options); |
5 | cout << " XML is valid" << endl; |
6 | } catch (std::exception& e) { |
7 | cout << "XML not valid: Exception Thrown = " << e.what() << endl; |
Optional: One can avoid printing the XML declaration at the beginning of the output by using the directive:
xml.options.setPrintXmlDeclaration(false);
Links:

Books: