preload

MoveChildElements (QTP, TestComplete, VBScript)

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

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

 

Description

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

Implementation


Public Function MoveChildElements(ByRef objXMLSrc, ByRef objXMLDest)

   If objXMLSrc is Nothing Then
    Set MoveChildElements = objXMLDest
    Exit Function
   End If

   If objXMLDest is Nothing Then
    Set objXMLDest = objXMLSrc.CloneNode(False)
   End If

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

   Set MoveChildElements = 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 = CreateChildElementByName(objXMLDoc2.DocumentElement, "Modules", "modules parent node dest")
  If objXMLParent2 is Nothing Then
    Call Reporter.ReportEvent(micFail, "CreateChildElementByName", "")
  End If

  Set objXMLParent2 = MoveChildElements(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 = 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

  Set objXMLParent2 = Nothing
  Set objXMLParent2 = MoveChildElements(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


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