XML Binding Development Guide : PART II Code Generation Examples : Chapter 5 SOAP Example : The SOAP Parsing Example
The SOAP Parsing Example
The parsing example, soap_main.cpp, demonstrates the use of rwsf::XmlReader to process content from an any element. To use this type of processing, you need to know the exact format of the XML data within the any element so you can know exactly the calls to make on the rwsf::XmlReader object.
This section describes the code in the provided example soap_main.cpp.
The 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. The example then uses an instance of rwsf::XmlReader to parse the element.
 
int main ()
{
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("soap.xml");
std::stringstream buffer;
buffer << istrm.rdbuf();
std::string xmlContents(buffer.str());
 
// Unmarshal the XML into the object model
e.unmarshal(xmlContents); // 2
 
tns::BodyType b = e.getBodyElement(); // 3
 
tns::BodyType::anyVector av = b.getAnyVector(); // 4
std::string s = av[0]; // 5
 
// process the entry with the reader
rwsf::XmlReader reader(s); // 6
reader.readElementStart(); // 7
reader.readNextNode(); // 8
 
std::cout << "Entry name: "
<< reader.getLastName().getQualifiedName() // 9
<< std::endl;
 
reader.readElementStart(); //10
 
std::cout << "Symbol name: "
<< reader.getLastName().getQualifiedName() //11
<< std::endl;
 
reader.readNextNode(); //12
 
std::cout << "Symbol value: "
<< reader.getLastContent() //13
<< std::endl;
 
reader.readElementEnd(); //14
reader.readElementEnd(); //15
 
} 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 holds the contents of the included 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, which the rwsf::XmlReader class can parse.
//6 Instantiates an rwsf::XmlReader that reads from the string.
//7 Reads the start of the next element in the string. The reader stores information about the element read.
//8 Reads the next child node of the current element. The reader stores information about the node read.
//9 Writes the qualified name of the last element name read to the standard output. This is the name of the element read on line //7.
//10 Reads the next element in the string. The reader stores information about the element read.
//11 Writes the qualified name of the last element name read to the standard output. This is the name of the element read on line //10.
//12 Reads the next child node of the current element. The reader stores information about the content read.
//13 Writes the content of the last element name read to the standard output. This is the content of the element read on line //12.
//14 Reads the end of the element started on line //10.
//15 Reads the end of the element started on line //7.