XML Binding Development Guide : PART II Code Generation Examples : Chapter 5 SOAP Example : The Trade Parsing Example
The Trade Parsing Example
The trade_main.cpp example demonstrates the use of classes generated by HydraExpress to process any elements. This example uses the trade.xsd schema to generate classes for the content of the SOAP body. This section describes the code in the example and presents instructions for running the code generator and compiling the example.
The trade example creates an instance of the class that represents the SOAP envelope, unmarshals the SOAP document into this instance, and retrieves a string that contains the first element in the SOAP body, as shown below:
 
int main(void)
{
tns::EnvelopeElement e; // 1
 
// Use a C++ try block to handle errors that can occur when
// unmarshaling XML.In this try block, we will output
// the error message to standard err
try {
std::ifstream istrm("trade.xml");
std::stringstream buffer;
buffer << istrm.rdbuf();
std::string xmlContents(buffer.str());
// Unmarshal the XML into the object model
e.unmarshal(xmlContents); // 2
 
// get the body from the envelope
tns::BodyType b = e.getBodyElement(); // 3
 
// get the body entries - there should be one
tns::BodyType::anyVector av = b.getAnyVector(); // 4
std::string s = av[0]; // 5
 
...
 
c::GetLastTradePrice tp; // 6
tp.unmarshal(s); // 7
std::cout << "Symbol value: " << tp.getSymbol() // 8
<< std::endl;
 
} catch (const rwsf::XmlParseException &e) {
std::cerr << "Parse error when unmarshaling : " << e.what()
<< std::endl;
return 1;
} catch (const rwsf::Exception &x) {
std::cerr << "Error : " << x.what() << std::endl;
return 1;
}
return 0;
}
//1 Constructs e as an empty EnvelopeElement instance.
//2 Unmarshals xmlContents into e. Note that xmlContents contains the text included in the trade.xml file.
//3 Retrieves the BodyElement contained within e. The SOAP body element holds the content of the SOAP message.
//4 Retrieves the vector of any elements from the SOAP body element. Since a SOAP body element can contain zero or more any elements, HydraExpress represents the contents of the SOAP body as a vector.
//5 Retrieves the first element of the vector. The string s is an XML element with arbitrary content.
//6 Constructs an empty GetLastTradePrice instance. The namespace qualifation c:: is needed because that is the defined namespace in trade.xsd.
//7 Unmarshals the string s into tp.
//8 Outputs the symbol to standard output.