Skip to content

Instantly share code, notes, and snippets.

@thmain
Created July 11, 2016 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/c733def1dfc3cfb658928d186f2ba020 to your computer and use it in GitHub Desktop.
Save thmain/c733def1dfc3cfb658928d186f2ba020 to your computer and use it in GitHub Desktop.
Sub ReadXML()
Call fnReadXMLByTags
End Sub
Function fnReadXMLByTags()
Dim mainWorkBook As Workbook
Set mainWorkBook = ActiveWorkbook
mainWorkBook.Sheets("Sheet1").Range("A:A").Clear
Set oXMLFile = CreateObject("Microsoft.XMLDOM")
XMLFileName = "D:\Sample.xml"
oXMLFile.Load (XMLFileName)
mainWorkBook.Sheets("Sheet1").Range("A1,B1,C1").Interior.ColorIndex = 40
mainWorkBook.Sheets("Sheet1").Range("A1,B1, C1").Borders.Value = 1
mainWorkBook.Sheets("Sheet1").Range("A" & 1).Value = "S.No"
mainWorkBook.Sheets("Sheet1").Range("B" & 1).Value = "Book Node"
mainWorkBook.Sheets("Sheet1").Range("C" & 1).Value = "Value"
Set Books = oXMLFile.SelectNodes("/catalog/book")
intCounter = 2
For i = 0 To Books.Length - 1
For j = 0 To Books(i).ChildNodes.Length - 1
mainWorkBook.Sheets("Sheet1").Range("A" & intCounter).Value = j + 1
mainWorkBook.Sheets("Sheet1").Range("B" & intCounter).Value = Books(i).ChildNodes(j).tagname
mainWorkBook.Sheets("Sheet1").Range("C" & intCounter).Value = Books(i).ChildNodes(j).Text
mainWorkBook.Sheets("Sheet1").Range("A" & intCounter).Borders.Value = 1
mainWorkBook.Sheets("Sheet1").Range("B" & intCounter).Borders.Value = 1
mainWorkBook.Sheets("Sheet1").Range("C" & intCounter).Borders.Value = 1
intCounter = intCounter + 1
Next
intCounter = intCounter + 1
Next
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment