Unmarshaling
The HydraExpress C++ generator creates a C++ API that allows you to unmarshal the XML shown in Purpose. Once the data is unmarshaled, you can analyze and manipulate the data using the type-safe C++ API. Finally, you can generate the XML that conforms to the above schema by taking the modified object and marshaling the object to XML.
Before you unmarshal the XML document, you must create an instance of the C++ type that corresponds with the root element of the XML document po.xml. In the purchase order example, this element is the purchaseOrder element. To create an instance of the purchaseOrder C++ type, instantiate the class that HydraExpress created for the type. In this case, the class is PurchaseOrder. The code sample below instantiates a PurchaseOrder object:
 
PurchaseOrder po;
To unmarshal an XML document, invoke the unmarshal() method on the object:
 
try {
std::ifstream istrm("po.xml");
std::stringstream buffer;
buffer << istrm.rdbuf();
std::string xmlContents(buffer.str());
po.unmarshal(xmlContents);
 
} catch (const rwsf::XmlParseException &e) {
std::cerr << "Parse error when unmashaling : " << e.what()
<< std::endl;
std::cerr << "Line number: " << e.getLineNumber() << std::endl;
std::cerr << "Column number: " << e.getColumnNumber()
<< std::endl;
return 1;
} catch (const rwsf::Exception &x) {
std::cerr << "Error : " << x.what() << std::endl;
return 1;
}
The code above opens a file called po.xml from the current working directory, reads the contents of that file into a std::ifstream object, and then asks the PurchaseOrder instance to unmarshal the XML. That is all it takes to use HydraExpress to parse the XML document.
Note that the code above shows the unmarshal method wrapped with a try-catch block. This allows the code to catch parsing and other errors and display an error message, as well as detail information about the particular error. Here is how to determine the kinds of exceptions you need to capture:
*You should always catch at least rwsf::Exception. All exceptions thrown by HydraExpress Library classes are derived from this class. The example catches the more specific rwsf::XmlParseExceptionso it can display a more specific error message.
*If you generate with the -stl option (the default), the implementation may need to catch std::exception. This example does not catch this exception because it does not use standard library classes.
*If you generated with the -sourcepro option (not used in this example), you may need to catch RWxmsg exceptions, which can be thrown by SourcePro C++ classes.