Skip to content

Instantly share code, notes, and snippets.

@thmain
Created July 11, 2016 03:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/2be0139d4c38f7985b4565c88c767aa9 to your computer and use it in GitHub Desktop.
Save thmain/2be0139d4c38f7985b4565c88c767aa9 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)
Set TitleNodes = oXMLFile.SelectNodes("/catalog/book/title/text()")
Set PriceNodes = oXMLFile.SelectNodes("/catalog/book/price/text()")
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 = "Book ID"
mainWorkBook.Sheets("Sheet1").Range("B" & 1).Value = "Book Titles"
mainWorkBook.Sheets("Sheet1").Range("C" & 1).Value = "Price"
mainWorkBook.Sheets("Sheet1").Range("D1").Value = "Total books: " & TitleNodes.Length
For i = 0 To (TitleNodes.Length - 1)
Title = TitleNodes(i).NodeValue
Price = PriceNodes(i).NodeValue
mainWorkBook.Sheets("Sheet1").Range("B" & i + 2).Borders.Value = 1
mainWorkBook.Sheets("Sheet1").Range("C" & i + 2).Borders.Value = 1
mainWorkBook.Sheets("Sheet1").Range("B" & i + 2).Value = Title
mainWorkBook.Sheets("Sheet1").Range("C" & i + 2).Value = Price
Next
'Reading the Attributes
Set Nodes_Attribute = oXMLFile.SelectNodes("/catalog/book")
For i = 0 To (Nodes_Attribute.Length - 1)
Attributes = Nodes_Attribute(i).getAttribute("id")
mainWorkBook.Sheets("Sheet1").Range("A" & i + 2).Borders.Value = 1
mainWorkBook.Sheets("Sheet1").Range("A" & i + 2).Value = Attributes
Next
End Function
Copy link

ghost commented Mar 11, 2019

Just what I was looking for. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment