Created
July 11, 2016 03:33
-
-
Save thmain/2be0139d4c38f7985b4565c88c767aa9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just what I was looking for. Thank you!