VBA XML - Lesson 63 - Operations On XML Elements
VBA XML - Lesson 63 - Operations On XML Elements
The IXMLDOMNode interface provides the properties and methods used to perform most routine operations of an XML document. The operations include locating a
node, adding a new tag, or deleting an element.
Before performing an operation, you will usually need to decide in what section of the document you want the action to be applied. As it happens, you have the root node,
a particular node inside the document, the parent of a node, the sibling of a node, etc. To get to a node, you will usually first get a reference to its IXMLDOMNode
object. To do this, you can declare an IXMLDOMNode variable, call a method that returns that reference, and initialize the variable with it. You can get a reference when
creating an element. Here is an example:
Private Sub cmdCreate_Click()
Dim docXMLDOM As DOMDocument
Dim nodElement As IXMLDOMElement
docXMLDOM.Save "C:\Exercises\videos2.xml"
You can also get a reference after locating the desired element.
Appending an Element
We already saw that, to let you create a new element, the DOMDocument class provides the createElement() method. Using this method, to create a new element, call it
and pass it the name of the element. We already know that the createElement() method belongs to the DOMDocument class. To let you create an element as either a
child of the root node or a child of any other element, the IXMLDOMNode interface is equipped with a method named appendChild. The syntax of the
IXMLDOMNode.appendChild() method is:
Public Function appendChild(ByVal newChild As IXMLDOMNode) As IXMLDOMNode
When calling this method, pass an IXMLDOMNode object you would have previously created. Here is an example:
Sider
https://www.functionx.com/vbaccess/Lesson63.htm 1/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
Set nodChild = docXMLDOM.createElement("video")
nodElement.appendChild nodChild
docXMLDOM.Save "C:\Exercises\videos3.xml"
In the same way, you can create other child nodes of the root. Simply use a combination of calling the DOMDocument.createElement() method that initializes
IXMLDOMElement element you want to create, and calling the IXMLDOMNode.appendChild() method to which you pass a reference of the IXMLDOMElement
variable. Here are examples:
docXMLDOM.Save "C:\Exercises\videos4.xml"
Notice that, if you simply call the IXMLDOMNode.appendChild() method to create an element, the newly added element is empty. If you want the new node to have a
value, assign the desired string to its Text property. Here is an example:
https://www.functionx.com/vbaccess/Lesson63.htm 2/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
nodChild.Text = "Basic Instinct"
nodElement.appendChild nodChild
docXMLDOM.Save "C:\Exercises\videos5.xml"
In the same way, you can add a value to any other element you are creating. If you want to create a processing instruction, declare its variable, call the
DOMDocument.createProcessingInstruction() method, then call the DOMDocument.appendChild() method. Here is an example:
Private Sub cmdCreateElement_Click()
Dim docXMLDOM As Object
Dim nodElement As Object
Dim piDeclaration As Object
docXMLDOM.Save "C:\Exercises\Houses2.xml"
docXMLDOM.Save "C:\Exercises\videos6.xml"
In the same way, you can create other child nodes of an element. Here are examples:
https://www.functionx.com/vbaccess/Lesson63.htm 3/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
Private Sub cmdCreateChildNodes_Click()
Dim docXMLDOM As DOMDocument
Dim nodElement As IXMLDOMElement
Dim nodChild As IXMLDOMElement
Dim nodGrandChild As IXMLDOMElement
docXMLDOM.Save "C:\Exercises\videos7.xml"
In the same way, you can create grand child nodes nested in child nodes of elements. Here are examples:
https://www.functionx.com/vbaccess/Lesson63.htm 4/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
Set nodChild = docXMLDOM.createElement("Video")
nodElement.appendChild nodChild
docXMLDOM.Save "C:\Exercises\videos8.xml"
Remember that, to specify the value of a node, assign the desired string to its Text property. Here are examples:
https://www.functionx.com/vbaccess/Lesson63.htm 5/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
nodChild.appendChild nodGrandChild
Set nodGreatGrandChild = docXMLDOM.createElement("CastMembers")
nodGrandChild.appendChild nodGreatGrandChild
docXMLDOM.Save "C:\Exercises\videos9.xml"
If you want to add a new node to the root of such an element, first get a reference to the root node using the documentElement property of the root node. To create
the new element, call the DOMDocument.createElement() method to create the start tag. Then call the IXMLDOMNode.appendChild() method on the root node.
Here is an example::
https://www.functionx.com/vbaccess/Lesson63.htm 6/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
docXMLDOM.Save "C:\Exercises\videos10.xml"
The call to IXMLDOMNode.appendChild() produces a reference to the new child node. If you want to create a child of that new node, after declaring an
IXMLDOMElement variable, initialize it with a call to DOMDocument.createElement(). Then call theIXMLDOMNode.appendChild() method of its parent and
pass your new node as argument.Here is an example of how you would do this:
Private Sub cmdCreate_Click()
Dim docXMLDOM As DOMDocument
Dim nodRoot As IXMLDOMElement
Dim nodElement As IXMLDOMElement
Dim nodChild As IXMLDOMElement
docXMLDOM.Save "C:\Exercises\Videos10.xml"
By now, you should know how easy it is to specify the value of an element: assign the desired string to its Text property. Just in case, here is an example:
Private Sub cmdCreate_Click()
Dim docXMLDOM As DOMDocument
Dim nodRoot As IXMLDOMElement
Dim nodElement As IXMLDOMElement
Dim nodChild As IXMLDOMElement
https://www.functionx.com/vbaccess/Lesson63.htm 7/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
docXMLDOM.Save "C:\Exercises\Videos10.xml"
In the same way, you can create child and grant-child (and great-grand child) nodes.
Locating an Element
Introduction
In some cases, you may want to perform an operation on an existing and particular node. For example, you may want to change the value of a node, you may want to add a
new child node to an existing node, etc. Before taking any of these actions, you must be able to locate or identify the desired element.
Locating an element consists of looking for a particular node among the nodes. To do this, you must start somewhere. Obviously, the first node you can identify is the
root. Once you get the root, you can then get a collection of its children. After getting a collection of the children of the root, you can locate a node in the collection. If
the node you are looking for is a child of that first collection, you can then get a collection of the child nodes of that node and proceed.
We saw that the IXMLDOMNodeList interface is equipped with an indexed property named Item. That member allows you to access an element based on its index.
Here are examples:
Private Sub cmdLocate_Click()
Dim docXMLDOM As DOMDocument
Dim nodRoot As IXMLDOMElement
Dim lstVideos As IXMLDOMNodeList
MsgBox lstVideos(0).Text
MsgBox lstVideos(0).XML
https://www.functionx.com/vbaccess/Lesson63.htm 8/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
You can use this characteristic to locate a node. Because XML is very flexible with the names (you can have two child nodes that have the same name) and values (you can
have two child nodes that have the same value) of nodes, when creating an XML file, it is your responsibility to create a scheme that would eventually allow you to uniquely
identify each element.
This method takes as argument the name of a node as a string. If at least one node that holds that name exists in the document, this method returns a collection of the
nodes with that name. If there is no node with that name, the collection is returned empty. Here is an example of calling the method:
Private Sub cmdLocate_Click()
Dim docXMLDOM As DOMDocument
Dim nodRoot As IXMLDOMElement
Dim lstVideos As IXMLDOMNodeList
Once you have a list of the nodes of a particular criterion, you can then act as you see fit. For example, For example, you can look for a particular node that holds a text of
your choice.
Imagine you want to create a new node as a child of one of the elements. The first action to take is to locate the element that will act as the
parent. If/since you know the element, you can locate it using its name by calling the DOMDocument.getElementsByTagName() method
applied to a collection of nodes. From that list of nodes, you can look for the node whose value you know. Once you have found this element,
get a reference to its parent. Then add the new node as a child to its parent. This can be done as follows:
https://www.functionx.com/vbaccess/Lesson63.htm 9/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
Private Sub cmdInsertElement_Click()
Dim Found As Boolean
Dim docXMLDOM As DOMDocument
Dim nodChild As IXMLDOMElement
Dim nodParent As IXMLDOMElement
Dim nodElement As IXMLDOMElement
Dim lstVideos As IXMLDOMNodeList
Found = False
Set docXMLDOM = New DOMDocument
docXMLDOM.Load "C:\Exercises\videos13.xml"
docXMLDOM.Save "C:\Exercises\Videos13.xml"
Found = True
End If
Next
This code creates an empty element. You may already know how to specify the value of an element: by assigning a string to its Text property.
Here is an example:
Private Sub cmdInsertElement_Click()
Dim Found As Boolean
Dim docXMLDOM As DOMDocument
Dim nodChild As IXMLDOMElement
Dim nodParent As IXMLDOMElement
Dim nodElement As IXMLDOMElement
Dim lstVideos As IXMLDOMNodeList
Found = False
Set docXMLDOM = New DOMDocument
docXMLDOM.Load "C:\Exercises\videos13.xml"
docXMLDOM.Save "C:\Exercises\Videos13.xml"
Found = True
End If
Next
https://www.functionx.com/vbaccess/Lesson63.htm 10/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
Creating an Element Before a Node
MSXML makes it possible to programmatically add a new element based on the position of another. For example, you can create a new node
before an existing one. To support this operation, the IXMLDOMNodeList interface is equipped with a method named insertBefore. Its
syntax is:
public Function insertAfter(ByVal newChild As IXMLDOMNodeList, _
ByVal refChild As IXMLDOMNodeList) As IXMLDOMNodeList
This function takes two arguments. The first argument is the element you want to create. The second argument is the node that will precede
the one you are creating. Consider the following XML file named videos.xml:
<?xml version="1.0" encoding="utf-8"?>
<videos>
<video>
<title>The Distinguished Gentleman</title>
<director>Jonathan Lynn</director>
<length>112 Minutes</length>
<format>DVD</format>
<rating>R</rating>
</video>
<video>
<title>Her Alibi</title>
<director>Bruce Beresford</director>
<length>94 Minutes</length>
<format>DVD</format>
<rating>PG-13</rating>
</video>
</videos>
Imagine you want to add a new element before the second one. Here is an example of how this can be done:
Private Sub cmdInsertElement_Click()
Dim Found As Boolean
Dim docXMLDOM As DOMDocument
Dim nodRoot As IXMLDOMElement
Dim nodElement As IXMLDOMElement
Dim lstVideos As IXMLDOMNodeList
Dim nodNewElement As IXMLDOMElement
Dim nodReference As IXMLDOMElement
If you want, you can then populate the new node with a value and/or child nodes. Here are examples from the original videos14.xml file:
Private Sub cmdInsertElement_Click()
Dim docXMLDOM As DOMDocument
https://www.functionx.com/vbaccess/Lesson63.htm 11/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
Dim nodRoot As IXMLDOMElement
Dim nodChild As IXMLDOMElement
Dim nodElement As IXMLDOMElement
Dim lstVideos As IXMLDOMNodeList
Dim nodReference As IXMLDOMElement
Dim nodNewElement As IXMLDOMElement
Of course, you can also add an element before a node that is a child node. Here is an example:
Private Sub cmdInsertElement_Click()
Dim docXMLDOM As DOMDocument
Dim nodElement As IXMLDOMElement
Dim lstVideos As IXMLDOMNodeList
Dim nodNewElement As IXMLDOMElement
https://www.functionx.com/vbaccess/Lesson63.htm 12/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
Next
Remember that you can specify the value of an element by assigning a string to its Text property. You can also add child nodes to the new
element.
For some reason, the MSXML library in Microsoft Access does not have a function to "insert after", but you should be able to easily perform
such an operation (or you can create the function yourself).
This mehod takes as arguments the new child node that will replace the existing element. Consider the following XML file named
videos15.xml
<?xml version="1.0" encoding="utf-8" ?>
<videos>
<video>
<title>The Distinguished Gentleman</title>
<director>Jonathan Lynn</director>
<length>112 Minutes</length>
<format>DVD</format>
<rating>R</rating>
</video>
<video>The video is provided in Blu-ray/DVD Combo.
<title>A Good Day to Die Hard</title>
<director>John Moore</Director>
<length>98 minutes</length>
</video>
<video>
<title>Her Alibi</title>
<director>Bruce Beresford</director>
<length>94 Minutes</length>
<format>DVD</format>
<rating>PG-13</rating>
</video>
</videos>
Imagine you want to completely replace the video in the middle: A Good Day to Die Hard. Here is an example that replaces an existing node
with a new one:
Private Sub cmdReplaceElement_Click()
Dim docXMLDOM As DOMDocument
Dim nodRoot As IXMLDOMElement
Dim nodChild As IXMLDOMElement
Dim nodElement As IXMLDOMElement
Dim lstVideos As IXMLDOMNodeList
Dim nodReference As IXMLDOMElement
Dim nodNewElement As IXMLDOMElement
https://www.functionx.com/vbaccess/Lesson63.htm 13/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
nodChild.Text = "Stephen Sielvert"
nodNewElement.appendChild nodChild
Set nodChild = docXMLDOM.createElement("producers")
nodChild.Text = "Kathleen Kennedy, George Lucas"
nodNewElement.appendChild nodChild
Set nodChild = docXMLDOM.createElement("rating")
nodChild.Text = "PG-13"
nodNewElement.appendChild nodChild
Imagine that the name of the director in the second video as Stephen Sielvert is wrong and you want to edit/change/update it. To do that
(programmatically), you would have to replace that string. To do this, you would have to locate the name and replace/change its value.
Obviously there are different approaches you can use but the whole technique (of course not the only one) is to call the
IXMLDOMNodeList.replaceChild() method after locating the video. Here is one way this can be done:
Private Sub cmdReplaceChild_Click()
Dim docXMLDOM As DOMDocument
Dim nodRoot As IXMLDOMElement
Dim nodVideo As IXMLDOMElement
Dim nodDirector As IXMLDOMElement
Dim lstDirectors As IXMLDOMNodeList
https://www.functionx.com/vbaccess/Lesson63.htm 14/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
' Use the new node in place of the old node
nodVideo.parentNode.replaceChild nodDirector, nodVideo
' Save the file
docXMLDOM.Save "C:\Exercises\Videos16.xml"
' Get out of the For loop (no need to look further)
Exit For
End If
Next
Using the same approach, you can edit/update/change/replace any node or child node or grand-child and/or their values.
If you have a node you don't want or don't need anymore in your XML document, you can delete it. To support this operation, the
IXMLDOMNode interface provides a method named removeChild. Its syntax is:
Public Function removeChild(ByVal node As IXMLDOMNode) As IXMLDOMNode
This method takes as argument the node to delete. If the node exists, it would be deleted and the method would return the node that was
deleted. If the node does not exist, nothing would happen. To effectively use this method, you should first locate the particular node you want
to delete. You can look for it using any of the logics we have applied so far. Once you find the node, you can then delete it. Consider the
following file named videos17.xml:
<?xml version="1.0" encoding="utf-8" ?>
<videos>
<video>
<title>The Distinguished Gentleman</title>
<director>Jonathan Lynn</director>
<length>112 Minutes</length>
<format>DVD</format>
<rating>R</rating>
</video>
<video>The video is provided in Blu-ray/DVD Combo.
<title>A Good Day to Die Hard</title>
<director>John Moore</director>
<length>98 minutes</length>
</video>
<video>
<title>Indiana Jones and the Kingdom of the Crystal Skull</title>
<director>Steven Spielberg</director>
<producers>Kathleen Kennedy, George Lucas</producers>
<rating>PG-13</rating>
</video>
<video>
<title>Her Alibi</title>
<director>Bruce Beresford</director>
<length>94 Minutes</length>
<format>DVD</format>
<rating>PG-13</rating>
</video>
</videos>
Imagine you want to delete the video titled A Good Day to Die Hard. Here is an example of calling this method to perform the operation:
Private Sub cmdDeleteElement_Click()
Dim docXMLDOM As DOMDocument
Dim nodRoot As IXMLDOMElement
Dim nodElement As IXMLDOMElement
Dim lstVideos As IXMLDOMNodeList
Found = False
Set docXMLDOM = New DOMDocument
docXMLDOM.Load "C:\Exercises\videos17.xml"
Set lstVideos = docXMLDOM.getElementsByTagName("title")
https://www.functionx.com/vbaccess/Lesson63.htm 15/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
docXMLDOM.Save "C:\Exercises\videos17.xml"
Exit For
End If
Next
Obviously, to delete the first or the last child node of an element, call either the firstChild() or the lastChild() method on the document
element. Here is an example that delete the first child node of the root element:
Private Sub cmdDeleteNode_Click()
Dim docXMLDOM As DOMDocument
Dim nodRoot As IXMLDOMElement
docXMLDOM.Save "C:\Exercises\videos17.xml"
Using any of these techniques, you can delete any node inside your XML document. Consider the above videos17.xml file. Imagine you want
to delete a node named producers in the video titled Kathleen Kennedy, George Lucas. Here is how this can be done:
Private Sub cmdDeleteChildNode_Click()
Dim docXMLDOM As DOMDocument
Dim nodProducer As IXMLDOMNode
Dim nodVideo As IXMLDOMElement
Dim lstVideos As IXMLDOMNodeList
Dim lstProducers As IXMLDOMNodeList
https://www.functionx.com/vbaccess/Lesson63.htm 16/17
25/01/24, 15:47 VBA XML - Lesson 63: Operations on XML Elements
<format>DVD</format>
<rating>R</rating>
</video>
<video>
<title>Indiana Jones and the Kingdom of the Crystal Skull</title>
<director>Steven Spielberg</director>
<rating>PG-13</rating>
</video>
<video>
<title>Her Alibi</title>
<director>Bruce Beresford</director>
<length>94 Minutes</length>
<format>DVD</format>
<rating>PG-13</rating>
</video>
</videos>
In the same way, you can delete a child of a child of a child node.
https://www.functionx.com/vbaccess/Lesson63.htm 17/17