CreateChildElementByName (QTP, TestComplete, VBScript)
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