Skip to content

How to Parse XML Data

All of these examples are applied to data that resides in a single workspace. This will be the most common type of data.

Example 1

Assume you have the following returned XML document:

<Customers>
    <Customer>
        <Name>3A Software</Name>
        <Number>13</Number>
        <State>CA</State>
    </Customer>
</Customers>

If you look at the client view helper in the "Data" window, you will see the line:

Document NameSpaceURI = http://www.dataaccess.com/Test/CustomerList

That's your NameSpaceURI. We'd parse this as follows:

Get wsCustInfo of hoMyClient "13" to hoXML
// if no data, we've had an error that has been reported.
If hoXML begin
    Move "http://www.dataaccess.com/Test/CustomerList" to sNS // the namespace
    Get DocumentElement of hoXML to hoRoot
    Get ChildElementValueNS of hoRoot sNS "Name" to sName
    Get ChildElementValueNS of hoRoot sNS "Number" to sNumber
    Get ChildElementValueNS of hoRoot sNS "State" to sState
    Send destroy of hoXML
end

Example 2

Assume you have the more complex data:

<Customers>
    <Customer>
        <Name>3A Software</Name>
        <Number>13</Number>
        <State>CA</State>
    </Customer>
    <Customer>
        <Name>Ace Manufacturers, Inc.</Name>
        <Number>4</Number>
        <State>IL</State>
    </Customer>
    <Customer>
        <Name>All Canada Brewing Company</Name>
        <Number>24</Number>
        <State>CA</State>
    </Customer>
</Customers>

Find the NameSpaceURI using the same technique as Example 1 and parse this as follows:

Get wsAllCust of hoMyClient to hoXML
// if no data, we've had an error that has been reported.
If hoXML begin
    Move "http://www.dataaccess.com/Test/CustomerList" to sNS // the namespace
    Get DocumentElement of hoXML to hoRoot // this is CustomerList
    Get ChildElementNS of hoRoot sNS "Customer" to hoCust
    While hoCust
        Get ChildElementValueNS of hoCust sNS "Name" to sName
        Get ChildElementValueNS of hoCust sNS "Number" to sNumber
        Get ChildElementValueNS of hoCust sNS "State" to sState
        Send DoThisCustomer sName sNumber sState
        Get NextElementNS of hoCust sNS "Customer" to hoCust
    end
    Send destroy of hoXML
end

You could also have used FirstChild and NextNode as follows:

Get wsAllCust of hoMyClient to hoXML
// if no data, we've had an error that has been reported.
If hoXML begin
    Move "http://www.dataaccess.com/Test/CustomerList" to sNS // the namespace
    Get DocumentElement of hoXML to hoRoot
    Get FirstChild of hoRoot to hoCust
    While hoCust
        Get ChildElementValueNS of hoCust sNS "Name" to sName
        Get ChildElementValueNS of hoCust sNS "Number" to sNumber
        Get ChildElementValueNS of hoCust sNS "State" to sState
        Send DoThisCustomer sName sNumber sState
        Get NextNode of hoCust to hoCust
    end
    Send destroy of hoXML
end

Example 3

Sometimes your element names might use prefixes (e.g., xmlns:m="SomeUrl"). If the namespace is defined with the same prefix and all of the elements are using this prefix, you follow the same instructions as above. For example:

<Customers xmlns:m="http://www.dataaccess.com/Test/CustomerList">
    <m:Customer>
        <m:Name>3A Software</m:Name>
        <m:Number>13</m:Number>
        <m:State>CA</m:State>
    </m:Customer>
</Customers>

You'd parse this in the exact same manner as Example 1. In fact, these two documents in Example 1 and 3 are identical.

Example 4

Assume you have the following returned XML document:

<Customers>
    <Customer>
        <Name>3A Software</Name>
        <Number>13</Number>
        <State>CA</State>
    </Customer>
</Customers>

This does not appear to have a namespace. It doesn’t. If you look at the client view helper in the "Data" window, you will see the line:

Document NameSpaceURI = ""

When there is no namespace defined, you are working in the global namespace which is identified as "". Therefore, use "" as your NameSpaceURI. We'd parse this as follows:

Get wsCustInfo of hoMyClient "13" to hoXML
// if no data, we've had an error that has been reported.
If hoXML begin
    Move "" to sNS // empty means the global namespace
    Get DocumentElement of hoXML to hoRoot
    Get ChildElementValueNS of hoRoot sNS "Name" to sName
    Get ChildElementValueNS of hoRoot sNS "Number" to sNumber
    Get ChildElementValueNS of hoRoot sNS "State" to sState
    Send destroy of hoXML
end

See Also

Handling XML Data