Hi Everyone,
Thought I'd post a solution to a task I've been working on in the hope it will
assist someone else with similar requirements. Thanks also to those who've
previously posted on this as I found the following to be most helpful as well:
http://www.mail-archive.com/u2-users@listserver.u2ug.org/msg18895.html
https://gdoesu2.wordpress.com/tag/xdomlocate/
http://listserver.u2ug.org/pipermail/u2-users/2010-September/004713.html
http://www.mvdeveloper.com/kb/docs/kb10.pdf
I had to parse some xml returned via a soap request to an IIS server:
?xml version=1.0?
s:Envelope xmlns:s=http://schemas.xmlsoap.org/soap/envelope/;
s:Body
SuggestResponse xmlns=http://tempuri.org/;
SuggestResult
xmlns:a=http://schemas.datacontract.org/2004/07/FirstMac.Services.Category.Contracts;
xmlns:i=http://www.w3.org/2001/XMLSchema-instance;
a:CategoryGroupSome group data/a:CategoryGroup
a:CategoryNameSome name data/a:CategoryName
/SuggestResult
/SuggestResponse
/s:Body
/s:Envelope
There are only ever 2 value nodes returned if the soap query is successful with
the SuggestResult node containing 'i:nil=true' if the query is not successful.
e.g. SuggestResult i:nil=true ... /
The xml contains a default namespace and after much searching and trial and
error I discovered I needed to use the following xpathString and nsMAP
parameters in the XDOMLocate function:
Notes re the XDOMLocate function.
I found the UV11.1 Basic Commands Reference and Basic Extensions documents for
XDOMLocate to be scarce on detail for the following:
* Each additional namespace in the string must also be space delimited.
* The double quotes enclosing the http url must be removed for each
namespace defined in the nsMAP parameter. The description given has double
quotes in the wrong places. e.g. if you have s:Envelope
xmlns:s=http://schemas.xmlsoap.org/soap/envelope/; then the namespace you
need to specify is nsMAP = 'xmlns:s=http://schemas.xmlsoap.org/soap/envelope/'
code
xpathStr = '/s:Envelope/s:Body'
nsMAP = 'xmlns:s=http://schemas.xmlsoap.org/soap/envelope/ '
nsMAP :=
'xmlns:a=http://schemas.datacontract.org/2004/07/FirstMac.Services.Category.Contracts
'
nsMAP := 'xmlns=http://tempuri.org/ '
nsMAP := 'xmlns:i=http://www.w3.org/2001/XMLSchema-instance'
OK = XDOMOpen( RespData, XML.FROM.STRING, hDOM )
if OK = XML.SUCCESS then
crt 'XML document opened'
OK = XDOMLocate( hDOM, xpathStr, nsMAP, hRoot )
if OK = XML.SUCCESS then
crt 'Found ':xpathStr
OK = XDOMGetNodeName( hRoot, NodeName )
crt 'Root node=':NodeName
OK = XDOMLocateNode( hRoot, XDOM.CHILD, XDOM.FIRST.CHILD,
XDOM.ELEMENT.NODE, hParent )
if OK = XML.SUCCESS then
OK = XDOMGetNodeName( hParent, NodeName )
crt 'Parent node = ':NodeName ;*
'SuggestResponse'
OK = XDOMLocateNode( hParent, XDOM.CHILD, XDOM.FIRST.CHILD,
XDOM.ELEMENT.NODE, hChild )
if OK = XML.SUCCESS then
OK = XDOMGetNodeName( hChild, NodeName )
crt 'Child node = ':NodeName ;*
'SuggestResult'
OK = XDOMLocateNode( hChild, XDOM.CHILD, XDOM.FIRST.CHILD,
XDOM.ELEMENT.NODE, hValueNode )
if OK = XML.SUCCESS then
OK = XDOMGetNodeName( hValueNode, NodeName )
crt 'Value node1 = ':NodeName ;*
'a:CategoryGroup'
OK = XDOMLocateNode(hValueNode, XDOM.CHILD, XDOM.FIRST.CHILD,
XDOM.TEXT.NODE, hTextNode )
if OK = XML.SUCCESS then
OK = XDOMGetNodeValue(hTextNode, Value )
crt 'Payload1 = ':Value ;* 'Some group
data'
end else
gosub ShowError
end
end
OK = XDOMLocateNode( hChild, XDOM.CHILD, 2, XDOM.ELEMENT.NODE,
hValueNode )
if OK = XML.SUCCESS then
OK = XDOMGetNodeName( hValueNode, NodeName )
crt 'Value node2 = ':NodeName ;*
'a:CategoryName'
OK = XDOMLocateNode(hValueNode, XDOM.CHILD, XDOM.FIRST.CHILD,
XDOM.TEXT.NODE, hTextNode )
if OK = XML.SUCCESS then
OK = XDOMGetNodeValue(hTextNode, Value )
crt 'Payload2 = ':Value ;* 'Some name
data'
end else
gosub ShowError
end
end else
gosub ShowError
end
end else
gosub ShowError
end
end else
gosub ShowError
end
end
OK = XDOMClose(hDOM)
end
return
*
ShowError:
OK = XMLGetError(Code, ErrText)
Crt Code = :Code
Crt Text = :ErrText
return
/code
Program Output:
XML document opened
Found /s:Envelope/s:Body
Root node=s:Body
Parent node = SuggestResponse
Child node = SuggestResult
Value node1 = a:TagGroup
Payload1 = Household expenses
Value node2 = a:TagName
Payload2 = Telephone / mobile
A