0

I'm trying to save all the repeated nodes to an array for my QTP automation.

When we use the below code we get all the repeated nodes present in the XML. Example:

strXmlPCoverage = /cts:InsuranceClaim/cts:MedicalClaim

Set nodes = xmlDoc.SelectNodes(strXmlPCoverage)
PathLength = nodes.Length

Let's say the PathLength returned "6" tags present in the XML as repeated tags.

/cts:InsuranceClaim/cts:MedicalClaim[0]
/cts:InsuranceClaim/cts:MedicalClaim[1]
/cts:InsuranceClaim/cts:MedicalClaim[2]
/cts:InsuranceClaim/cts:MedicalClaim[3]
/cts:InsuranceClaim/cts:MedicalClaim[4]
/cts:InsuranceClaim/cts:MedicalClaim[5]

Now I want to save all those 6 different paths to array. I'm not able to identify the property that displays the values stored in nodes.

Set nodes = xmlDoc.SelectNodes(strXmlPCoverage)
PathLength = nodes.Length
For Each NodeItem In nodes
    ArrAllNodes(i) = NodeItem.nodeValue
Next

The above code stores value of that node to Array instead of the node itself. Can you please help me how to store the nodes to array instead of node values

Output displayed by the code:

ArrAllNodes(0) = abc
ArrAllNodes(1) = xyz
...

Output Expected:

ArrAllNodes(0) = /cts:InsuranceClaim/cts:MedicalClaim[0]
ArrAllNodes(1) = /cts:InsuranceClaim/cts:MedicalClaim[1]
...

1 Answer 1

1

The NodeValue property gives you the value of a node. What you're looking for is the path of the node. DOM objects don't have a method/property providing that information, so you need to determine the path yourself by traversing upwards in the DOM tree (via the ParentNode property).

Something like this might give you a starting point:

Function HasSiblings(n)
    HasSiblings = (TypeName(n.PreviousSibling) = "IXMLDOMElement") Or _
                  (TypeName(n.NextSibling) = "IXMLDOMElement")
End Function

Function GetIndex(n)
    If n.PreviousSibling Is Nothing Then
        GetIndex = 0
    Else
        GetIndex = GetIndex(n.PreviousSibling) + 1
    End If
End Function

Function GetPath(n)
    path = "\" & n.NodeName

    'add index if node has siblings
    If HasSiblings(n) Then
        path = path & "[" & GetIndex(n) & "]"
    End If

    'add parent path if current node is not the document element
    If TypeName(n.ParentNode) = "IXMLDOMElement" Then
        path = GetPath(n.ParentNode) & path
    End If

    GetPath = path
End Function

The above code is just a sample with plenty room for improvement, though. For instance, it doesn't check if siblings actually have the same node name.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ansgar. Will look into this.

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.