preload

CopyChildElements (QTP, TestComplete, VBScript)

Posted by Albert Gareev on Dec 08, 2011 | Categories: Source codeXML Data

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

Description

Copy all child elements (and attributes, comments, text nodes) from one XML node to another.

Implementation

Public Function CopyChildElements(ByRef objXMLSrc, ByRef objXMLDest)
   Dim objCloneNode
 
   If objXMLSrc is Nothing Then
    Set MoveChildElements = objXMLDest
    Exit Function
   End If

   If objXMLDest is Nothing Then
    Set objXMLDest = objXMLSrc.CloneNode(True)
    Set CopyChildElements = objXMLDest
    Exit Function
   End If

   Set objCloneNode = objXMLSrc.CloneNode(True)

   While Not (objCloneNode.FirstChild is Nothing)
    Call objXMLDest.AppendChild(objCloneNode.FirstChild)
   Wend

   Set objCloneNode = Nothing

   Set CopyChildElements = objXMLDest

End Function

Test Code

  Set objXMLDoc = CreateXMLDOM("test")
  Set objXMLParent = CreateChildElementByName(objXMLDoc.DocumentElement, "Modules", "modules parent node")
  If objXMLParent is Nothing Then
    Call Reporter.ReportEvent(micFail, "CreateChildElementByName", "")
  End If
  Set objXMLNode = CreateChildElementByName(objXMLParent, "Macro", "macro node 1")
  If objXMLNode is Nothing Then
    Call Reporter.ReportEvent(micFail, "CreateChildElementByName", "")
  End If
  Set objXMLNode = CreateChildElementByName(objXMLParent, "Macro", "macro node 2")
  If objXMLNode is Nothing Then
    Call Reporter.ReportEvent(micFail, "CreateChildElementByName", "")
  End If
  Set objXMLNode = CreateChildElementByName(objXMLParent, "Macro", "macro node 3")
  If objXMLNode is Nothing Then
    Call Reporter.ReportEvent(micFail, "CreateChildElementByName", "")
  End If

  sText = "<root>text</root>"
  Set objXMLDoc2 = CreateXMLDOMFromString(sText)
  If objXMLDoc2 is Nothing Then
    Call Reporter.ReportEvent(micFail, "CreateXMLDOMFromString", "")
  End If

  Set objXMLParent2 = Nothing
  Set objXMLParent2 = CopyChildElements(objXMLParent, objXMLParent2)

  Set objXMLNode = ChildElementByName(objXMLParent2, "Macro") 
  If objXMLNode is Nothing Then
    Call Reporter.ReportEvent(micFail, "ChildElementByName", "")
  End If
  If objXMLNode.BaseName <> "Macro" Then
    Call Reporter.ReportEvent(micFail, "ChildElementByName", "")
  End If
  If objXMLNode.Text <> "macro node 1" Then
    Call Reporter.ReportEvent(micFail, "ChildElementByName", "")
  End If
  Set objXMLNode = ChildElementByName(objXMLParent, "Macro") 
  If objXMLNode is Nothing Then
    Call Reporter.ReportEvent(micFail, "ChildElementByName", "")
  End If
  If objXMLNode.BaseName <> "Macro" Then
    Call Reporter.ReportEvent(micFail, "ChildElementByName", "")
  End If
  If objXMLNode.Text <> "macro node 1" Then
    Call Reporter.ReportEvent(micFail, "ChildElementByName", "")
  End If

  Set objXMLParent2 = CreateChildElementByName(objXMLDoc2.DocumentElement, "Modules", "modules parent node dest")
  If objXMLParent2 is Nothing Then
    Call Reporter.ReportEvent(micFail, "CreateChildElementByName", "")
  End If
  Set objXMLParent2 = CopyChildElements(objXMLParent, objXMLParent2)
  Set objXMLNode = ChildElementByName(objXMLParent2, "Macro") 
  If objXMLNode is Nothing Then
    Call Reporter.ReportEvent(micFail, "ChildElementByName", "")
  End If
  If objXMLNode.BaseName <> "Macro" Then
    Call Reporter.ReportEvent(micFail, "ChildElementByName", "")
  End If
  If objXMLNode.Text <> "macro node 1" Then
    Call Reporter.ReportEvent(micFail, "ChildElementByName", "")
  End If
  Set objXMLNode = ChildElementByName(objXMLParent, "Macro") 
  If objXMLNode is Nothing Then
    Call Reporter.ReportEvent(micFail, "ChildElementByName", "")
  End If
  If objXMLNode.BaseName <> "Macro" Then
    Call Reporter.ReportEvent(micFail, "ChildElementByName", "")
  End If
  If objXMLNode.Text <> "macro node 1" Then
    Call Reporter.ReportEvent(micFail, "ChildElementByName", "")
  End If


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.