Friday 25 October 2013

Excel Macro to process xml file


Sub processXML()

'online ref - http://msdn.microsoft.com/en-us/library/aa468547.aspx
Set xDoc = CreateObject("MSXML2.DOMDocument")
'to load the xml string
'xDoc.LoadXML ("<book><str>hello</str></book>")
'to load xml from file, use below syntax
xDoc.Load ("c:\abc.xml")
'Display all elements and their values
Call DisplayNode(xDoc.ChildNodes, 0)
MsgBox str1
'We can also use xpath to get the collection of nodes
xDoc.setProperty "SelectionLanguage", "XPath"
strPath = "//book"
Set nodelist = xDoc.DocumentElement.SelectNodes(strPath)
MsgBox "No of Nodes Found by using xpath is -> " & nodelist.Length & " Node"
Set xDoc = Nothing
End Sub


Public Sub DisplayNode(ByRef Nodes, ByVal Indent)
'Recursive function to traverse all the nodes inside xml file/ String
Dim xNode
Indent = Indent + 2
For Each xNode In Nodes
If xNode.NodeType = NODE_TEXT Then
str1 = str1 & " " & xNode.ParentNode.nodeName & ":" & xNode.NodeValue
End If
If xNode.HasChildNodes Then
DisplayNode xNode.ChildNodes, Indent
End If
Next
End Sub


What do you think on above excel macro topic? Please provide your input, comments, questions on excel macro. I will try to reply as soon as possible. I value your opinion on Excel Macros.

No comments:

Post a Comment

Popular Posts

Search This Blog