Recurse XML file using vbscript

后端 未结 2 1145
借酒劲吻你
借酒劲吻你 2020-12-04 01:13

I am trying to use recursive technique to retrieve xml file. Can somebody help me doing that.

I am trying to read below xml, but not sure the depth of the nested tag

2条回答
  •  星月不相逢
    2020-12-04 01:26

    You need a recursive Sub to traverse the XML document tree. In principle:

      Dim oFS    : Set oFS  = CreateObject("Scripting.FileSystemObject")
      Dim sFSpec : sFSpec   = oFS.GetAbsolutePathName("..\data\so14975608.xml")
      Dim oXML   : Set oXML = CreateObject("Msxml2.DOMDocument.6.0")
      oXML.load sFSpec
      If 0 = oXML.parseError Then
         recursiveTraversal oXML.documentElement, 0
      Else
         WScript.Echo objMSXML.parseError.reason
      End If
    
    Sub recursiveTraversal(oElm, nIndent)
      WScript.Echo Space(nIndent), oElm.tagName
      If 0 < oElm.childNodes.length Then
         Dim oChild
         For Each oChild In oElm.childNodes
             recursiveTraversal oChild, nIndent + 2
         Next
      Else
         If 0 < oElm.attributes.length Then
            Dim oAttr
            For Each oAttr In oElm.attributes
                WScript.Echo Space(nIndent + 1), oAttr.name, oAttr.value
            Next
         End If
      End If
    End Sub
    

    output for your sample data:

     TestSuites
       TestSuite
         TestCase
           TestStep
            TSName TestStep 1
           TestStep
            TSName TestStep 2
         TestCase
           TestStep
            TSName TestStep 1
           TestStep
            TSName TestStep 2
       TestSuite
        SuiteName Smoke
       TestSuite
        SuiteName Sanity
    

    Based on a more detailed plan - what information do you need to extract/process - you must study some suitable XML documentation (start here) to determine the functions/properties to put in the above skeleton.

    P.S.:

    People who did not buy the above won't profit from this.

提交回复
热议问题