Yolinux.com Tutorial

C++ XmlBeansxx

XML schema based data processing through auto-generated C++ code.

The XmlBeansxx framework was inspired by the Apache XML Beans Java framwork.

 |  Home Page |  Linux Tutorials |  Terms |  Privacy Policy |  Advertising |  Contact  | 

Related YoLinux Tutorials:

°C++ Info, links

°C++ String Class

°C++ STL vector, list

°C++ Log4cxx logging

°MS/Visual C++ Practices

°C++ Memory corruption and leaks

°YoLinux Tutorials Index




Free Information Technology Magazines and Document Downloads
TradePub link image


Free Information Technology Software and Development Magazine Subscriptions and Document Downloads


XmlBeansxx 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. XmlBeansxx will generate C++ classes and library which can be compiled in or linked into your C++ program.


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
  • 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
  • gmp (http://swox.com/gmp/)
    • RHEL5 (gmp 4.1.4): yum install gmp
    • Ubuntu 8.10 (libgmpxx4 4.2.2, libgmp3-dev: 4.2.2): sudo apt-get install libgmpxx4 libgmp3-dev
  • boost and boost-devel
    • RHEL5 (boost: 1.33.1): yum install boost boost-devel
    • Ubuntu 8.10 (libboost-dev: 1.34.1): sudo apt-get install libboost-dev
  • 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 not Xerces-C 3.0.1.)
    • 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)
    • Ubuntu: Compile from source and install to /usr
  • 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 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 recognised by the XmlBeans build in any other location such as /opt or /usr/local.

Build and install:

  • Build and install Log4cxx to /usr
  • tar xzf xmlbeansxx-0.9.8.tar.gz
  • cd xmlbeansxx-0.9.8
  • ./bootstrap
  • ./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 fuction. (As Far As I Can Tell)
  • 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)
  • make install
    Installs to /usr/include/xmlbeansxx/, /usr/lib, /usr/bin and /usr/share/xmlbeansxx/ for configure "--preifix=/usr".

Links:


XmlBeansxx Example:

The following schema example is based on the Apache XML Beans example.

File: EasyPO.xsd
<!--
Copyright 2004 The Apache Software Foundation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 -->
<xs:schema
targetNamespace="http://xmlbeans.apache.org/samples/enumeration/schemaenum/easypo"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:po="http://xmlbeans.apache.org/samples/enumeration/schemaenum/easypo"
    elementFormDefault="qualified">
    <xs:element name="purchase-order">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="customer" type="po:customer"/>
                <xs:element name="date" type="xs:dateTime" />
                <xs:element name="line-item" type="po:line-item" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element name="shipper" type="po:shipper" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="customer">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="address" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="line-item">
        <xs:sequence>
            <xs:element name="description" type="xs:string"/>
            <xs:element name="per-unit-ounces" type="xs:decimal"/>
            <xs:element name="price" type="xs:double"/>
            <xs:element name="quantity" type="xs:int"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="shipper">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="per-ounce-rate" type="xs:decimal"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>
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

File: testEasyPO.cpp
#include "EasyPO.h"
#include <iostream>
#include <fstream>

using namespace std;
using namespace xmlbeansxx;
using namespace xmlbeansxx::samples::enumeration::schemaenum::easypo;

int main() {

    try {
        fstream in("easypo.xml", ios::in);
        PurchaseOrderDocument pDoc(PurchaseOrderDocument::Factory::parse(in));

        xmlbeansxx::XmlOptions opts;
        opts.setValidation(true);

        vector<LineItem> arr = pDoc.getPurchaseOrder().getLineItemArray();
        for(int i=0; i < arr.size() ; i++) {
            cout << "item: " << i << endl;
            cout << " - description:     " << arr[i].getDescription() << endl;
            cout << " - quantity:        " << arr[i].getQuantity() << endl;
            cout << " - price:           " << arr[i].getPrice() << endl;
            cout << " - amount:          " << arr[i].getQuantity() * arr[i].getPrice() << endl;
        }

    // Debug line cout << "Xml:" << endl << pDoc.toString() << endl;

    } catch (BeansException &ex) {
        cout << "BeansException: " << ex.getMessage() << endl;
    }
    return 0;
}
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)

File: easypo.xml
<?xml version="1.0" encoding="UTF-8"?>
<purchase-order
xmlns="http://xmlbeans.apache.org/samples/enumeration/schemaenum/easypo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <customer>
        <name>Larry Kowalski</name>
        <address>ul. Anytown, PA</address>
    </customer>
    <date>2005-01-01T12:00:00</date>
    <line-item>
        <description>Burnham's Celestial Handbook Vol 1</description>
        <per-unit-ounces>10</per-unit-ounces>
        <price>23</price>
        <quantity>1</quantity>
    </line-item>
    <line-item>
        <description>Burnham's Celestial Handbook Vol 2</description>
        <per-unit-ounces>7</per-unit-ounces>
        <price>22</price>
        <quantity>1</quantity>
    </line-item>
    <line-item>
        <description>Introduction to Linux</description>
        <per-unit-ounces>7</per-unit-ounces>
        <price>27</price>
        <quantity>1</quantity>
    </line-item>
    <shipper>
        <name>Zip Ship</name>
        <per-ounce-rate>0.65</per-ounce-rate>
    </shipper>
</purchase-order>

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


BooksBooks:

C++ How to Program
by Harvey M. Deitel, Paul J. Deitel
ISBN #0131857576, Prentice Hall

Fifth edition. The first edition of this book (and Professor Sheely at UTA) taught me to program C++. It is complete and covers all the nuances of the C++ language. It also has good code examples. Good for both learning and reference.

Amazon.com
Professional XML Development with Apache Tools : Xerces, Xalan, FOP, Cocoon, Axis, Xindice
by Theodore W. Leung
ISBN #0764543555, Wrox Press

Amazon.com

    Bookmark and Share





YoLinux.com Home Page
YoLinux Tutorial Index | Terms
Privacy Policy | Advertise with us | Feedback Form |
Unauthorized copying or redistribution prohibited.
YoLinux Tutorials. Linux Information Portal includes informative tutorials and links to many Linux sites.


Bookmark and Share

Copyright © 2009, 2010 by Greg Ippolito