XMLNode2Dictionary (QTP, TestComplete, VBScript)

Posted by Albert Gareev on Jul 19, 2011 | Categories: Source codeXML Data

Parent page: Service Functions – MSXMLDOM (QTP, TestComplete, VBScript)

Service Functions – Dictionary (QTP, TestComplete, VBScript)


Create Dictionary object and populate with data (only direct content – text, comments, CData, and attributes) taken from XML node.

Naming convention:

  • @attr_name for attributes;
  • #text_index for text nodes;
  • #comment_index for comment nodes;
  • #cdata_index for CData Section nodes.



Public Function XMLNode2Dictionary(ByRef objXMLElement)
  Dim objDictionary
  Dim objColl, objAttr, Iter
  Dim intCommentCount, intTextCount, intCDataCount
  Set objDictionary = CreateObject("Scripting.Dictionary")
 If objXMLElement is Nothing Then
    objDictionary.Item("#innertext") = ""
  Set XMLNode2Dictionary = objDictionary
  Exit Function
 End If
  objDictionary.Item("#innertext") = objXMLElement.Text
  intCommentCount = 0
  intTextCount = 0
  intCDataCount = 0

  Set objColl = objXMLElement.ChildNodes
  For Iter=0 To objColl.length-1
    Select Case objColl.Item(Iter).NodeName
      Case "#comment"
        intCommentCount = intCommentCount + 1
        objDictionary.Item("#comment_"&intCommentCount) = objColl.Item(Iter).NodeValue
      Case "#text"
        intTextCount = intTextCount + 1
        objDictionary.Item("#text_"&intTextCount) = objColl.Item(Iter).NodeValue
      Case "#cdata-section"
        intCDataCount = intCDataCount + 1
        objDictionary.Item("#cdata_"&intCDataCount) = objColl.Item(Iter).NodeValue
      Case Else
    End Select
  For Each objAttr in objXMLElement.Attributes
    objDictionary.Item("@"&objAttr.Name) = objAttr.Value
 Set XMLNode2Dictionary = objDictionary
End Function

  • Leave a Reply

    * Required
    ** Your Email is never shared

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.