Handling XML Data
Normally, complex data is passed back and forth as structs and arrays. When structs and arrays are used, you never have to deal with the actual underlying XML data. In some cases, the data will be passed or returned as XML. This occurs when either:
- The web service specifically passes the data as XML, or
- The data passed could not be reduced to a single fundamental native data type (e.g., string), struct, or array.
When such data is used, the data is passed and returned as an XML object.
Most of the time, you will never have to deal directly with XML data in web services. If you do, this section explains how it is done.
If you are working with XML data, most often you will be dealing with return data. In such a case, the data is returned as a handle to an XML object. You will need to parse this data, use it as needed, and then destroy the XML object.
Parsing this kind of data is surprisingly simple, if you know how to do it. To work successfully with the data, you need to:
- Know how the XML document data is structured.
- Know the full expanded name of each element. An expanded name consists of the NameSpaceURI and the BaseName.
- Know what methods to use to get at the data.
See Also
The Document Data Structure
You can see what the data looks like (solving 1 and 2 above) by looking at the returned data. Use the Web Client Helper View to see this data. Add WebClientHelper.vw to the top of your program and enter Set phoSoapClientHelper to oClientWSHelper inside your client object.
It should be noted that the proper way to know a document’s structure is to look at its schema definition (located in the WSDL). Reading through a schema definition can be daunting. Most of the time, you can just look at the document and see the data structure.
BaseName and NameSpaceURI
The BaseName is the name within the element tags. If the element contains a prefix, just look at the name to the right of the “:”. An element prefix consists of a name followed by a colon, which is followed by the BaseName. If no prefix exists, the BaseName is the entire element name.
You can find the NameSpaceURI in several ways. We will start by discussing a method that will work most of the time. The NameSpaceURI is a name that usually looks like a web address, such as http://www.dataaccess.com/Test/CustomerList. (It’s probably not a real address - URLs are used because they provide a good way to create a unique name.) In most cases, the return data will all have the same NameSpaceURI. If you look at the client helper view, you will see that it displays this information (look for “Document NameSpaceURI = xxxx”). Most of the time, you can use this name if all of the elements have the same prefix (e.g., all elements start with x) or if none of the elements have a prefix.
In some cases, the data will have multiple namespaces. In such a case, you need to review the section titled "Multiple Namespaces in an XML Document".
For now, let's assume you've got single namespace data.
The XML Parsing Methods
The XML classes have a number of namespace-aware messages that make it easy to work with namespaces. You want to use these messages. There is a tendency for developers to attempt to ignore the namespaces and to work with an element’s prefix and BaseName. Avoid this tendency, as it tends to create solutions that seem to work and then proceed to get very messy. Methods are available that make it very easy to parse the documents in the proper manner.
The main methods you will use to parse your return data will be:
Get DocumentElement
This returns the root element of your document.
Get DocumentElement of hoXML to hoRoot
You always work from this root. The first thing to do with any XML Document is to get the root element. All of your parsing will use this root object as your starting point.
Get FirstChild
This returns the first child node of an element.
Get FirstChild of hoRoot to hoNode
Get NextNode
This replaces the current XML node with the next node (i.e., its sibling node).
Get NextNode of hoNode to hoNode
Get ChildElementNS
This returns the first child node that matches an expanded name (NameSpaceURI and BaseName).
Get ChildElementNS of hoRoot sNameSpace "Customer" to hoCust
Get NextElementNS
This replaces the current XML node with the next node that matches an expanded name.
Get NextElementNS of hoCust sNameSpace "Customer" to hoCust
Get ChildElementValueNS
This returns an element's value for an expanded name.
Get ChildElementValueNS of hoCust sNameSpace "name" to sName
Send Destroy
This destroys XML nodes when you are done with them.
Send Destroy of hoXML
If you know the structure of your XML document and you know an element’s BaseName and NameSpaceURI, you can use these few messages to handle most of your parsing.