0

I am trying to get some node value in XML using VBScript. If I use the normal XML with the below script, then it is correctly fetching the expected node value. But if I use the SOAP response which is having namespaces, then below script is throwing the following error in the highlighted line:

Object Required: 'nNode'

Script:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set xmlDoc = CreateObject("Microsoft.XMLDOM")

xmlDoc.Load editName
xmlDoc.SetProperty "SelectionNamespaces", "xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:sch='http://www.exchangerate.com/webservices/schemas' xmlns:xbe='http://www.exchangerate.com/rate'"

Set nNode = xmlDoc.SelectSingleNode(tag)

objSheet.Cells(i, Column).value = nNode.text  '<-- this fails

strResult = xmlDoc.Save(editName)   

How can I solve this?

Sample Response XML:

enter image description here


Input:

tag="/SOAP-ENV:Envelope/SOAP-ENV:Body/sch:Request/sch:Response/xbe:ConversionRateResult"
halfer
  • 19,824
  • 17
  • 99
  • 186
Subburaj
  • 2,294
  • 3
  • 20
  • 37
  • The parser needs to know about your namespaces, try something like `xmlDoc.setProperty "SelectionNamespaces", "your namespace list here"` before calling `SelectSingleNode()`. From what are can see there are at least three different namespaces the parser will need to know about for that XPath to work `soap`, `sch` and `xbe`. – user692942 Nov 04 '16 at 09:16
  • Possible duplicate of [How to query an XML file with namespaced attributes](http://stackoverflow.com/questions/22334541/how-to-query-an-xml-file-with-namespaced-attributes) – user692942 Nov 04 '16 at 09:21
  • @Lankymart, I have added the namespace and still it is not working. For your Reference, I have modified the above script with the Name Space. My SOAP Response XML Format and Linked Query SOAP Response XML Format is somewhat different. So, I couldn't solve this problem – Subburaj Nov 04 '16 at 18:52
  • The XML in your sample is invalid. It declares the `xbe` namespace only for the tag `MessageType`, but tries to use it in other tags as well. Either declare the namespace on each tag where it's used or declare it on a common parent tag. The `Load` method fails silently, so check `xmlDoc.ParseError` and `xmlDoc.ParseError.Reason` to detect such errors. Also, `Microsoft.XMLDOM` is deprecated. Use the more recent `Msxml2.DOMDocument.6.0` object. – Ansgar Wiechers Nov 05 '16 at 14:15
  • Thank you all for your comments!!!.Now i have solved the problem using the Relative XPath instead of absolute Xpath. I have changed the input tag as 'tag="//xbe:ConversionRateResult"' and it is working fine for me – Subburaj Nov 05 '16 at 16:50

1 Answers1

1

We have to use the relative Xpath to parse the SOAP Response and the above problem is solved by using the relative Xpath as tag="//xbe:ConversionRateResult".

Subburaj
  • 2,294
  • 3
  • 20
  • 37