How to Parse XML in Microsoft Excel VBA
This article will teach us how to parse XML files in VBA.
Importance of an XML Parser
As a Microsoft Excel user, it is common that you might receive some data in the form of an XML file. You will have to retrieve the information from the XML file and use it in your sheets or VBA macros according to your requirement.
A way to do this is to treat it as a text file and parse the information. But this is not an elegant way to parse XML files since the information is stored well-structured using tags, and treating it as a text file negates this concept.
Therefore, we will have to make use of an XML Parser. An XML Parser reads the XML file and retrieves the relevant data so it can be used readily.
Build XML Parser Using VBA
We can parse an XML file using VBA and convert the data into our Excel sheet. The method we will be using uses the XML DOM
implementation, short for the XML Document Object Model, and this model allows us to represent the XML file as an object we can then manipulate as required.
To start parsing your XML file through VBA, you must perform a simple sequence of steps. These are explained below.
To parse XML through VBA, you need to have MSXML.4.0
or greater on your system.
-
Add Reference to Microsoft XML
First, you need to add a reference to
Microsoft XML, V6.0
in the VBA Editor. This is how it is done:Open the VBA Editor from the Developer tab in Excel.
-
In the menu, go to Tools > References.
-
Scroll down and check
Microsoft XML, V6.0
, then click onOK
.Note that the version of Microsoft XML depends on the operating system and Microsoft Office installed on your computer.
-
Write VBA Code to Load the XML File Into
XML DOM
Suppose we have the following XML file:
<?xml version="1.0" encoding="ISO8859-1" ?> <menu> <food> <name> Halwa Puri </name> <price> $7.50 </price> <description> Halwa Puri is from Indian and Pakistani cuisines, having the sweet Halwa and the savory Puri which is a fried flatbread. </description> <calories> 900 </calories> </food> </menu>
We can use the following code to parse this XML file through VBA by making an
XML DOM
object in the following way:Sub XMLParser() Dim xDoc As New MSXML2.DOMDocument60 Dim node As IXMLDOMElement Set xDoc = New MSXML2.DOMDocument60 With xDoc .async = False .validateOnParse = True If xDoc.Load("D:\VBA\example.xml") = False Then Debug.Print .parseError.reason, .parseError.ErrorCode Exit Sub End If Set node = xDoc.SelectSingleNode("//price") MsgBox node.Text End With End Sub
In the code above, we have first created a variable xDoc
of the MSXML2.DOMDocument60
type. Here, we have appended 60
at the end because we are using version 6.0
of Microsoft XML
, and without the 60
, this code will generate a compile-time error of User-defined type not found
.
Next, we have specified that we are working with the xDoc
variable using the With
statement. The .async
property defines permission for asynchronous downloads, and the .validateOnParse
property indicates if the parser should validate the XML
document.
After that, we use the .Load
function to load the specified XML
file into the DOM
variable. Here, you can change the path and file name to the one on your computer.
The next two lines are for error handling in case the XML
file is not loaded properly. To test if the loading has worked, we take one node from the file and specify its name as price
.
You should note that the node name is case-sensitive and must be specified according to your XML
file. Finally, we display the price using the node.Text
property in a message box.
Output:
This shows that the loading has worked perfectly fine.
One way to use the XML file data is to store it in an Excel sheet. Let us make a few changes to the code above to store the data in the Excel sheet:
Sub XMLParser()
Dim xDoc As New MSXML2.DOMDocument60
Set xDoc = New MSXML2.DOMDocument60
Dim list As MSXML2.IXMLDOMNodeList
Dim osh As Worksheet
Set osh = ThisWorkbook.Sheets("Sheet1")
oRow = 1
With xDoc
.async = False
.validateOnParse = True
If xDoc.Load("D:\VBA\example.xml") = False Then
Debug.Print .parseError.reason, .parseError.ErrorCode
Exit Sub
End If
Set list = xDoc.SelectNodes("//price")
loopCount = 0
For Each node In list
oRow = oRow + 1
osh.Range("A" & oRow) = node.Text
Next
End With
End Sub
Here, we are retrieving all the price
nodes and storing them in the sheet. In this example, we have only one price
node that will be saved into the sheet as follows:
You can tweak the code according to your XML file and requirements.
Conclusion
This sums up our discussion on the method to parse XML
files through VBA. In this article, we have learned how to build an XML parser using XML DOM
in VBA.
Hello, I am Bilal, a research enthusiast who tends to break and make code from scratch. I dwell deep into the latest issues faced by the developer community and provide answers and different solutions. Apart from that, I am just another normal developer with a laptop, a mug of coffee, some biscuits and a thick spectacle!
GitHub