Friday, October 15, 2010

Find the XML Node path based on node name

We can find Xpath for given node in XML file using with below two functions.
First function:--
Call ExtractProductCode

Function ExtractProductCode
Environment.Value("columnExcel")=3
Set doc = XMLUtil.CreateXML()
doc.LoadFile "C:\FileName.xml"
Set root = doc.GetRootElement
Environment.Value("ParentName") = root.ElementName()
'Set children = root.ChildElements()
'Msgbox children.Count()
call XMLDataExtract1(root.ChildElements(), root.ChildElements().Count())
End Function



Function XMLDataExtract1(children, cnt)
c1= children.Count()

If cnt > 0 Then
For i= 1 to cnt 'children.Count()
Set firstChild = children.Item(i)
c2 = firstChild.ElementName()
c3 = firstChild.Value()
If Trim(CStr(c2)) = Trim(DataTable.Value("NodeName","Global")) Then '
XMLPath= ""
While firstChild.ElementName() <>"soapenv:Envelope"
XMLPath= "/" & CStr(firstChild.ElementName()) & XMLPath
Set firstChild= firstChild.Parent()
Wend
XMLPath = "/" & Environment.Value("ParentName") & XMLPath
DataTable.Value(cInt(Environment.Value("columnExcel")),"Global") = XMLPath
Environment.Value("columnExcel")= cInt(Environment.Value("columnExcel"))+1
Exit Function
End If
If firstChild.ChildElements().Count() > 0 Then
Set children = firstChild.ChildElements()
call XMLDataExtract1(children,children.Count())
End if
Next
End If
Reporter.Filter = 3
'MsgBox firstChild.Parent().Parent().ElementName()
On Error Resume Next
Set children = firstChild.Parent().Parent().ChildElements()
On Error Goto 0
Reporter.Filter = 0
End Function

No comments:

Post a Comment