Monday, January 28, 2008

How to get the parent node of the given node in XML using VBscript?

Actually this program i have written with the help of functions in VBscript .... so try to analyse it carefully....

Dim snode_name,xml_file
xml_file = InputBox("Enter xml file")
'snode_name = InputBox("Enter Any Node")
Call xmltoexcel(xml_file)


Function xmltoexcel(xmlfile)
Dim i,j,snode_name
i=2
j=2
Set xlApp = CreateObject("Excel.Application")
xlApp.visible = true
Set objWorkbook = xlApp.Workbooks.Add("C:\Documents and Settings\BhavanaA\Desktop\VBscript\Book3.xls")
set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.load(xmlfile)
Call displaynode(xmlDoc.documentElement,xlApp,i)
snode_name = InputBox("Enter Any Node")
Call specific_node(xmlDoc.documentElement,xlApp,j,snode_name)

'objWorkbook.close
'xlApp.quit
End Function


Function displaynode(ByVal node_name,ByRef xlApp,Byref i )

xlApp.Range("A" & 1).Value="NODENAME"
xlApp.Range("B" & 1).Value="DATA"
xlApp.Range("C" & 1).Value="PARENTNODE"
xlApp.Range("A" & i).Value = node_name.nodename
xlApp.Range("B" & i).Value = ""
xlApp.Range("C" & i).Value = node_name.parentnode.nodename
For each cnt in node_name.childNodes
If cnt.HasChildNodes Then
i=i+1
Call displaynode(cnt,xlApp,i)
Else
xlApp.Range("B" & i).Value = cnt.text
xlApp.Range("C" & i).Value = cnt.parentnode.parentnode.nodename
End If
Next
End Function

Function specific_node(ByVal node_name,ByRef xlApp,ByRef j,snode_name)
xlApp.Range("H" & j).Value = node_name.nodename
'MsgBox iNode.nodename
If node_name.HasChildNodes Then
For Each sChildNode In node_name.ChildNodes
If sChildNode.HasChildNodes then
j=j+1
Call specific_node(sChildNode,xlApp,j,snode_name)
Else
xlApp.Range("I" & j).Value = sChildNode.parentnode.nodename
xlApp.Range("J" & j).Value = sChildNode.text
End if
Next
End If
If snode_name = node_name.nodename Then
Wscript.echo "My parent is " & node_name.parentnode.nodename
If node_name.HasChildNodes Then
get_nodelist(node_name)
End If
End If
End Function

Function get_nodelist(snode)
For Each strnode In snode.ChildNodes
If strNode.HasChildNodes then
snode_list = snode_list & "--->"& strnode.parentnode.nodename
Call get_nodelist(strnode)

End If
Next
Wscript.echo snode_list
End Function


Any QUES fEEL FREE TO ASK
Here is my email- id : aditya_joy58@yahoo.co.in

No comments: