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 first creates an instance of the class that represents the SOAP envelope, unmarshals the SOAP document into this instance, then 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("po.xml"); std::stringstream buffer; buffer << istrm.rdbuf(); std::string xmlContents(buffer.str()); // Unmarshal the XML into the object model e.unmarshal(xmlContents.data()); // 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].data(); // 5 ... GetLastTradePrice tp; // 6 tp.unmarshal(s.data()); // 7 std::cout << "Symbol value: " << tp.getSymbol() // 8 << std::endl; } catch (const rwsf::XmlParseError &e) { std::cerr << "Parse error when unmarshaling : " << 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; } return 0; }
//1 | Constructs e as an empty EnvelopeElement instance. |
//2 | Unmarshals xmlContents into e. Note that xmlContents contains the text included in the soap.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. |
//7 | Unmarshals the string s into tp. |
//8 | Outputs the symbol to standard output. |
©2004-2007 Copyright Quovadx, Inc. All Rights Reserved.
Quovadx and Rogue Wave are registered trademarks of Quovadx, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.