Vba loading xml documents in Excel
In the previous chapter, we saw the basics of XML documents and its applications.
The XML documents are the most preferred format for data sharing.
In this chapter, we will discuss how to work with XML documents in VBA.
The first step is to load the XML document in the program, which is as discussed below.
To work with XML in VBA, the XMLDOM object has to be referrenced and loaded in the program.
The following code shows, how to load a XMLDOM object.
- Dim xmlDoc As Object
- Set xmlDoc=CreateObject("Microsoft.XMLDOM")
Here, DOM stands for Document Object Model.
The DOM defines the document structure of XML and also it speficifies the ways to access the XML.
With the DOM, programmers can create and build documents, navigate their structure, and add, modify, or delete elements and content.
However DOM interfaces for the internal subset and external subset have not yet been specified.
In DOM, documents have a logical structure which is very much like a tree.
The term "DOM" was chosen because it is an "object model" and is used in the traditional object oriented design sense.
The "CreateObject" method is used to create a referrence to any kind of object.
There are various COM components in Excel and they can be accessed using this method.
In our Example, We have created XMLDOM object.
Next, to load the XML dicument, the following code is used.
As in the above code, the Load method is used to initialize XML document.
Another way of loading the XML document is as shown below
This is done by MSXML2.DomDocument namespace.
This is illustrated as follows:
- Dim XMLDOC As MSXML2.DOMDocument
- XMLDOC.Load (<xml_file>)