Forum Discussion

Arcano_Sombrio's avatar
Arcano_Sombrio
New Contributor
9 years ago

How to get node value of XML using function getElementsByTagName on VBScript?

I'm utilizing version 11.0.644 of TestComplete.

XML:
<tag1>
     <tag2>
         <mod>Test </mod>
     </tag2>
</tag1>

I'm using this code for obtain the node value from XML:

Sub XMLTest
Set XMLTestReader = Sys.OleObject("Msxml2.DOMDocument.6.0")
XMLTestReader.async = false
XMLTestReader.load(XML.XML_Teste.Document)

Set nodeText = XMLTestReader.getElementsByTagName("mod").item(0).childNodes.item(0).nodeValue
BuiltIn.ShowMessage(nodeText)

End Sub

The XML is present in Stores/XML/XML_Teste.

This code returns error.
How to obtain the correct node value?

  • ghuff2's avatar
    ghuff2
    9 years ago

    Okay, this line of your code:

     

    XMLTestReader.load(XML.XML_Teste.Document)

     

    The load method of MSXML takes a filepath to an XML file as the argument. XML.XML_Teste.Document will return a DOM object that represents the XML, not the filepath. If you want you can pass in the actual file path to the XML file, but Document property of an XML checkpoint already return a DOM object like what you want. So your code could just be like this:

     

    Sub XMLTest
    Set nodeText = XML.XML_Teste.Document.getElementsByTagName("mod").item(0).text
    BuiltIn.ShowMessage(nodeText)

    I use python so hopefully that's all the correct syntax.

     

     

     

  • djadhav's avatar
    djadhav
    Regular Contributor

    Try this :

     

    Set nodeText = XMLTestReader.getElementsByTagName("mod").item(0).childNodes.item(0).Text

  • The error message is attached.
    Even with the code passe by djadhav, the error still occurs.

    • ghuff2's avatar
      ghuff2
      Contributor

      Okay, this line of your code:

       

      XMLTestReader.load(XML.XML_Teste.Document)

       

      The load method of MSXML takes a filepath to an XML file as the argument. XML.XML_Teste.Document will return a DOM object that represents the XML, not the filepath. If you want you can pass in the actual file path to the XML file, but Document property of an XML checkpoint already return a DOM object like what you want. So your code could just be like this:

       

      Sub XMLTest
      Set nodeText = XML.XML_Teste.Document.getElementsByTagName("mod").item(0).text
      BuiltIn.ShowMessage(nodeText)

      I use python so hopefully that's all the correct syntax.

       

       

       

    • djadhav's avatar
      djadhav
      Regular Contributor

      Remove the keyword SET from the beginning of the statement.

       

      nodeText = XMLTestReader.getElementsByTagName("mod").item(0).childNodes.item(0).Text

  • Using the python functions work and the script returns the value os the node without problems.
    I will follow using python in the tests.
    Thanks for cooperation.