Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
HydraExpress XML Binding Development Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

4.4 The Example Code

As in the previous chapter, let's look at the code to gain a better understanding of how HydraExpress manipulates the XML.

4.4.1 C++ Namespaces

If the schema has a target namespace and a prefix associated with the target namespace URI, then a C++ namespace that matches the prefix is used. Note that, in the provided po1_main.cpp sample application, HydraExpress includes the C++ types that HydraExpress created from the po namespace:

For more information about C++ namespaces in code generated by HydraExpress, see Section 8.2.2.

4.4.2 Unmarshaling

HydraExpress generated a C++ API that allows us to unmarshal the XML above. Once the data is unmarshaled, we can then analyze and manipulate the data using the type-safe C++ API. Finally, we can generate the XML that conforms to the above schema by taking the modified tree and marshaling it to XML.

Before unmarshaling the XML document, we must create an instance of the C++ type that corresponds with the root element of the XML document. In the purchase order example, this element is the purchaseOrder element, which is namespace qualified, as shown in the creation of the instance:

To unmarshal an XML document, read it into the implementation and invoke the unmarshal() method on the instance.

//1This and the following three lines read in the XML document.
//2Calls the unmarshal() method of the PurchaseOrder instance to unmarshal the XML document.

Note that the code above is nearly identical to that in the basic example. Adding the XML namespace qualification does not affect the use of the generated API. HydraExpress takes care of the underlying details.

4.4.3 Manipulating XML Content in C++

Since the structure of this example is exactly the same as that in the basic example, we will expand a bit on the basic example. In this example, we add a new item to the purchase order instance that was unmarshaled before marshaling it back out.

The purchase order schema defines a type called Items which has a child element named item of type Item, which can occur an unlimited number of times. HydraExpress maps this definition to an std::vector of the type Item. A typedef on the Items class is generated with the type name concatenated with the word Vector, which can be used to reference the vector type.

The code below illustrates how this collection can be used to add a new toaster to the purchase order. We first get the old list and store a copy of it to the local variable named itemVector, as shown here:

We then create a new item that represents a toaster we plan to purchase, and we set the details that correspond to that toaster:

We append this new item to the item vector before creating the new Items instance that was defined in the purchase order schema. The only member on the Items instance is an item vector, which we set to the modified vector instance, as shown here:

4.4.4 Marshaling

We can then call the marshal() method on the root object. The code below illustrates this step and prints the result to standard out.



Previous fileTop of DocumentContentsIndex pageNext file

©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.