Browse DevX
Sign up for e-mail newsletters from DevX


Manipulate XML Documents in C++ with xmlbeansxx : Page 2

Using XML processing typically simplifies a developer's job--but not when you're programming in c++. Ever wish someone would create the create the C++ equivalent of XMLBeans? Someone has. Find out how this new open source tool fills a serious gap in the C++ software environment.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Coding with xmlbeansxx
xmlbeansxx consists of two parts, a code generator and a library. The code generation is straightforward: it takes an XML schema document as an input and produces a shared library with classes representing an XML document. Create a new directory, copy a schema definition file (the EasyPO.xsd file from the Apache XMLBeans tutorials or xmlbeansxx distribution) inside, and issue:

scompxx EasyPO.xsd

You have just created a library project with your beans. Compile and install it with the Autotools mantra: ./bootstrap, ./configure, ./make, ./make install.

Figure 1. The Class Diagram: This class diagram shows the classes generated from the purchase order XML Schema along with the basic structure and relations between generated classes and the xmlbeansxx library.

The example in this article uses the generated library to process XML documents containing orders to a bookstore. Customers place their orders online. The orders are then sent to a warehouse and processed. The program produces a printout containing the shipment method, price, and extra items for person assembling the parcel.

The following classes were generated for a purchase order schema: Shipper, LineItem, and Customer. For complex types, we generated PurchaseOrderDocument. This class can also be used for a purchase order global element. The _PurchaseOrder class was generated for the nested anonymous complex type used inside the purchase order element.

Reading Documents
After you've generated a library, you can read the sample document and analyze its content. You'll have to create a purchase order document file like this one:

<?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>Jan Kowalski</name> <address>ul. Marszalkowska, Warszawa</address> </customer> <date>2005-01-01T12:00:00</date> <line-item> <description>Zwodniczy punkt</description> <per-unit-ounces>10</per-unit-ounces> <price>23</price> <quantity>1</quantity> </line-item> <line-item> <description>Paw krolowej</description> <per-unit-ounces>7</per-unit-ounces> <price>22</price> <quantity>1</quantity> </line-item> <line-item> <description>Zahir</description> <per-unit-ounces>7</per-unit-ounces> <price>27</price> <quantity>1</quantity> </line-item> <shipper> <name>Poczta</name> <per-ounce-rate>0.65</per-ounce-rate> </shipper> </purchase-order>

Now you are ready to write a few lines of code to make sure everything is working fine:

#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); //declaration and construction of document object PurchaseOrderDocumentPtr poDoc(PurchaseOrderDocument::Factory::parse(in)); poDoc->serialize(cout); cout << endl; } catch (BeansException ex) { cout << "BeansException: " << ex.getMessage() << endl; } return 0; }

You should see a serialized document on standard output. The file was parsed and beans objects were populated with simple data from XML. Upon serialization, the XML document was generated from the beans objects' hierarchy.

This example is fairly simple, but it's just an introduction. To write more complex code, you can iterate over purchase order items, write them out, and calculate amounts. To modify the above code, add the following lines just below the declaration and construction of poDoc:

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

After running the program, you should see a list of items, the quantities to be assembled, and prices on your console.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date