preload

CreateChildElementByName (QTP, TestComplete, VBScript)

Posted by Albert Gareev on Jun 25, 2008 | Categories: Source codeXML Data

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

Description

Create and add (append) child element node to parent with the tagname and inner text specified.

Implementation

Public Function CreateChildElementByName(ByRef objXMLParent, ByVal sTagName, ByVal sInnerText)
  Dim boolRC, intRC
 Dim objXMLDoc, objXMLNode, objTextNode

 If objXMLParent is Nothing Then
  Set CreateChildElementByName = Nothing
  Exit Function
 End If

  Set objXMLDoc = objXMLParent.OwnerDocument
  On Error Resume Next
  Set objXMLNode = objXMLDoc.CreateElement(sTagName)
  intRC = Err.Number
  On Error GoTo 0
  boolRC = isNull(objXMLNode) OR (intRC <> 0) 
  If boolRC Then
    Set CreateChildElementByName = Nothing
    Exit Function
  End If    
 
  Set objTextNode = objXMLDoc.CreateTextNode(sInnerText)
  objXMLNode.AppendChild(objTextNode)
  objXMLParent.AppendChild(objXMLNode)

 Set CreateChildElementByName = objXMLNode
 
End Function

Test Code

  Set objXMLParent = CreateChildElementByName(objXMLDoc.DocumentElement, "case", "test case")
  If objXMLParent is Nothing Then
    Log.Error "CreateChildElementByName failed"
  End If
  Set objXMLNode = CreateChildElementByName(objXMLParent, "step", "test step")
  If objXMLParent is Nothing Then
    Log.Error "CreateChildElementByName failed"
  End If
  If objXMLNode.BaseName <> "step" Then
    Log.Error "CreateChildElementByName failed"
  End If
  If objXMLNode.Text <> "test step" Then
    Log.Error "CreateChildElementByName failed"
  End If


  • 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.