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 itema 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++.