Forum Discussion

maandim's avatar
maandim
New Contributor
11 years ago

selectSingleNode using vbscript


Below is the structure of my xml file:

<configuration>


  <appSettings>


    <add key="ProductVersion" value="5.5.5"/>


    <add key="LogsDirectory" value="e:\\Logs"/>


  </appSettings>


....


<configuration>



I am trying following code to get value of LogsDirectory:




configurationFilePath = "e:\conf.xml"


Set xmlDoc = CreateObject("MSXML2.DomDocument.6.0")


xmlDoc.async = false


Call xmlDoc.load(configurationFilePath)


 


xpath1 = ".//configuration/appSettings/add[@key='LogsDirectory']/@value"


LogsDirectory = xmlDoc.selectSingleNode(xpath1)



But it is giving error as object required.



Any help highly appreciated.



Thanks



 




3 Replies

  • paul_scroce's avatar
    paul_scroce
    Frequent Contributor
    Hi



    selectSingleNode can be in vbscript, but you need to use the set command when assigning an object reference to a variable ...

    Set LogsDirectory = xmlDoc.selectSingleNode(xpath1)



    Paul
  • maandim's avatar
    maandim
    New Contributor
    Found one sample in online help manual and changed my xml file. Removed xpath syntax and used For loop.

    It looks like Testcomplete doesn't support xpath in vbscrtipt whereas xpath is supported in Jscript.
  • aLostDawg's avatar
    aLostDawg
    Occasional Contributor
      Xpath is supported in VBScript in TestComplete. I use it almost exclusively to find elements in web pages that may be dynamic at runtime. And it prevents me from having to build a name mapping that is enormous. The methods in VBScript in TestComplet are a little different. You'll probably want to use the FindChildByXPath method. Do a quick search on 'xpath' in the TC help and you'll find more info, but here's what I would try. CAVEAT: I'm using this to find objects on webpages, not in pure XML files, so it might be slightly different. 



      You will want to make sure that your base object (xmlDoc) is correct because that can throw you off. I think the last '/' in your xpath1 may be throwing it off. Just try to locate the node without using the '@value'. Then once you have that node, then ask for the '@value'. Also, it's easy to forget, but you have to use 'Set [object variable name]' to assign the node object to an object variable in VBScript. 



    'set your xpath variable

    xpath1 = ".//add[@key = 'LogsDirectory']"

    'execute the FindChildByXPath method and assign the object to LogsDirectory

    Set LogsDirectory = xmlDoc.FindChildByXPath(xpath1, False)

    'now that you have the node object get the value of the 'value' attribute

    sLogsDirectoryValue = LogsDirectory.GetAttribute("value", text)






      Again, this may be a little different since I'm using it on a webpage and you're using it on pure XML, but hopefully it's close enough to get you what you need. 





    JC