1

I am traversing through xml using recursion approach and want to make sure if particular node have any attributes. here is what I have tried

First method: Get list of all attributes using foreach loop, but problem with this approach is if any node is not having any attribute it throws error

Second approach: finding the attribute by attribute name passed as parameter and verify if it returns null, but in this case I need to have all the attribute names handy which never let me go ahead with recursive approach.

1 Answer 1

1

People who looked at this will buy:

The first approach is correct. A bare bones implementation:

  Dim oFS    : Set oFS = CreateObject("Scripting.FileSystemObject")
  Dim sFSpec : sFSpec  = oFS.GetAbsolutePathName("..\data\so15218800.xml")
  Dim oXML   : Set oXML = CreateObject("Msxml2.DOMDocument.6.0")
  oXML.load sFSpec
  If 0 = oXML.parseError Then
     recursiveTraversalAtt oXML.documentElement, 0
  Else
     WScript.Echo objMSXML.parseError.reason
  End If

Sub recursiveTraversalAtt(oElm, nIndent)
  WScript.Echo Space(nIndent), oElm.tagName
  If 0 < oElm.childNodes.length Then
     If 0 < oElm.attributes.length Then showAttr oElm, nIndent
     Dim oChild
     For Each oChild In oElm.childNodes
         recursiveTraversalAtt oChild, nIndent + 2
     Next
  Else
     If 0 < oElm.attributes.length Then showAttr oElm, nIndent
  End If
End Sub

Sub showAttr(oElm, nIndent)
  Dim oAttr
  For Each oAttr In oElm.attributes
      WScript.Echo Space(nIndent + 1), oAttr.name, oAttr.value
  Next
End Sub

output:

 TestSuites
   TestSuite
    SuiteName Regression
    TCName TestCase 1
     TestCase
      TCName TestCase 1
      abc 123
       TestStep
        TSName TestStep 1
       TestStep
        TSName TestStep 2
       NoAttr
         TestSuite
          SuiteName Regression
          TCName TestCase 1
           TestStep
            TSName TestStep 1
     TestCase
      TCName TestCase 2
       TestStep
        TSName TestStep 1
       TestStep
        TSName TestStep 2
   TestSuite
   TestSuite
    SuiteName Sanity
Sign up to request clarification or add additional context in comments.

1 Comment

Also I am trying to get the test suite, test case and test steps into tht dicitonary object(nested dictionary), well don't know how the dictionary performs. if you have insight on this. Please throw some light.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.