Browse DevX
Sign up for e-mail newsletters from DevX


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

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

Modifying Documents
As you probably know, modifying XML documents is usually the more difficult part of a project. This is true in DOM, but not if you're using data binding tools. To illustrate this, you will make a modification to the purchase order XML document. Before writing out the purchase order content, you will add one more item—a free gift for all happy customers.

LineItemPtr giftLineItem = poDoc->getPurchaseOrder()->addNewLineItem(); giftLineItem->setDescription(string("Calendar")); giftLineItem->setPrice(0); giftLineItem->setQuantity(1); giftLineItem->setPerUnitOunces(10);

Validating Documents
At present, validation is done by Xerces-X and requires the parsing of the document. To do this, you have to create an XMLParser, set the validation to true, and parse the document:

XmlParser parser; parser.getXmlOptions()->setValidation(true); istringstream iss(poDoc->toString()); PurchaseOrderDocumentPtr validPoDoc = PurchaseOrderDocument::Factory::newInstance(); try { parser.parse(iss, validPoDoc.get()); } catch (BeansException& ex) { std::cerr << "Exception while parsing:" << ex.what() << endl; }

You can also validate on the first parsing of the document. Add these lines at the beginning of code to enable validation:

XmlOptionsPtr opts(new XmlOptions()); opts->setValidation(true); PurchaseOrderDocumentPtr poDoc(PurchaseOrderDocument::Factory::parse(in, opts));

As you can see, using xmlbeansxx is fairly convenient. It provides a simple means for handling XML documents defined in XML Schema. The availability of an open source XML-to-C++ binding tool fills a serious gap in the software environment for XML processing in C++.

Witek Wolejszo is a programmer/analyst for TouK s.c. in Warsaw, Poland, a firm focused on service-oriented architectures and workflow systems for finance.
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