Introduction to XML in Microsoft Windows |
|
Introduction to the MSXML Library
Introduction
The Extensible Markup Language, or XML, is standardized by the W3C (http://www.w3c.org) organization. To make it possible to use XML in its operating systems, Microsoft created a library named MSXML, which stands for Microsoft XML Core services. Because Microsoft XML Core services is a separate library, to use it in Microsoft Access, you must "load" it. To do this, you have two options. One option is to graphical load the library. Another option is to programmatically do it
To graphically load MSXML, on the main menu of Microsoft Visual Basic, click Tools -> References... In the References dialog box, click the check box of the latest version of Microsoft XML:
Click OK. To programmatically load MSXML, you should first (make sure you get the latest release of the MSXML library from the Microsoft web site and) know the version of MSXML installed on your computer or the version that is installed in your users#039; computers. Declare a variable of type Object. To initialize it, call the global CreateObject() function and pass it the MSXML library you want to use. This can be done as follows:
Private Sub cmdLoadXMLLibrary_Click()
Dim xmlDocument As Object
Set xmlDocument = CreateObject("MSXML2.DOMDocument.3.0")
Set xmlDocument = Nothing
End Sub
From then, you can use Microsoft XML Core services in your Microsoft Access project.
Interfaces, Classes, and Objects in MSXML
Like a traditional programming library, Microsoft XML Core services provides many types of objects. As you may know already, a class is a building block or a way to describe what an object looks like. It is like the architural design of a house. It is not the house itself but it gives you a good idea of how a house built from that design should look. A class is supposed to be complete in the sense that it gives a complete description (or provides a complete structural description) of the objects that will be created or built from it.
Another type of object in a library is called an interface. An interface is like a class, which specifies the structure of an object. In the traditional programming sense, an interface is (usually) a (n incomplete) structure from which actual classes can be built (we say that it is an abstract from which actual classes can be created). This means that, in the traditional sense of computer programming, an interface is a vague description of a structure. An example of an interface would be a residence (is it a house? A commercial building? An appartment complex? A moving house? A tent? etc). Once an interface exists, more appropriate and precise classes are created from it (such as houses, tents, etc). By tradition, the name of an interface starts with I (this distinguishes it from a class, but this is not a rule, just an indication).
Without going through all the boring (and probably uncessary) details (there is no way we can go through all the issues to know about object-oriented programming (OOP) here), the few things to know are that:
The MSXML library provides many classes and interfaces. To use any of them, you can (in most cases, you must) declare a variable of the desired type and appropriated initialize it.
Introduction to the Document Object Model
While creating an XML document, you must follow some rules in order to get a valid document. The XML rules are standardized by the W3C Document Object Model (DOM). To support these standards, Microsoft XML Core services provides a class named DOMDocument that makes it possible to start an XML document, to populate it with the desired contents, and to perform many other related operations on the contents of the document.
Before using a DOM, declare a variable of type DOMDocument and initialize it using the New operator. This can be done as follows:
Private Sub cmdCreateDOMDocument_Click()
Dim docXMLDOM As DOMDocument
Set docXMLDOM = New DOMDocument
End Sub
This causes the DOM object to be loaded into the computer's memory and start using resources. Eventually, after using the object, you should remove it from memory. This is done by assigning Nothing to it. This can be done as follows:
Private Sub cmdCreateDOMDocument_Click()
Dim docXMLDOM As DOMDocument
Set docXMLDOM = New DOMDocument
Set docXMLDOM = Nothing
End Sub
Providing XML Code Using DOMDocument
If you already know or have the XML code you want to use, the DOMDocument class allows you to provide it. This is done using one of the class's methods named loadXml. Its syntax is:
Public Function loadXml(ByVal bstrXML As String) As Boolean
This method takes a String as argument and returns a Boolean value. The DOMDocument.loadXml() method doesn't create an XML file, it only allows you to provide or create XML code. The code can be created as argument. You can also first declare and initialize a String variable with the XML code, then pass it as argument to the DOMDocument.loadXml() method.
Saving/Opening an XML File
Saving an XML Document
Probably the most common way to create an XML document in Microsoft Windows consists of using Notepad or any other text editor. After opening the text editor, you can enter the necessary lines of code. After creating the file, you must save it. When saving it, you can include the name of the file in double-quotes. You can also first set the Save As Type combo box to All Files and then enter the name of the file with the .xml extension.
Saving a DOM Object
If you call the DOMDocument.loadXml() method, only the XML code is created, not the file. To let you create the Windows file, the DOMDocument is equipped with a method named save(). Its syntax is:
Public Sub save(ByVal FileName As String)
This method takes one argument that can be a valid file name and must include the .xml extension. You can also pass the whole path where the file must be located.
Opening an XML File
Probably the easiest way to open an XML file is with a text editor such as Notepad. You can also open the file using a Web browser or an appropriate application that recognizes XML code.
To let you open an XML file, the DOMDocument class is equipped with a method named load. Its syntax is:
Public Function load(ByVal FileName As String)
The method takes as argument the name or path of the file. Here is an example of calling it:
Private Sub cmdCreateDOMDocument_Click()
Dim docXMLDOM As DOMDocument
Set docXMLDOM = New DOMDocument
docXMLDOM.Load "music.xml"
Set docXMLDOM = Nothing
End Sub
You can also provide a complete path to the file.
Introduction to the Structure of an XML Document
Introduction to XML Nodes
An XML document appears as an upside-down tree with many branches and leaves. Each of these objects is called a node. To support the nodes of an XML document, the MSXML library provides an interface named IXMLDOMNode. This is the ancestor to all types of nodes.
Introduction to Node Types
An XML document can contain various types of nodes. The categories or possible types of nodes are identified by an enumeration named DOMNodeType. Each of the different types of nodes we will use has a corresponding member in that enumeration.
Giving Processing Instructions
As you may be aware already, an XML document can start with the type of character scheme it is using. This is known as the encoding declaration. That declaration gives instructions to the parser on the types or groups of characters and symbols used in your document. This is done with a line as follows:
<?xml version="1.0" encoding="utf-8"?>
Remember that, in your VBA code, you can use the single-quote inside a string, or you can double the double-quote. Here are examples:
Private Sub cmdCreateProcessingInstructions_Click()
Dim docXMLDOM As DOMDocument
Set docXMLDOM = New DOMDocument
docXMLDOM.loadXML "<?xml version=""1.0"" encoding=""utf-8""?>"
Set docXMLDOM = Nothing
End Sub
To support processing instructions, the MSXML library provides an interface named IXMLDOMNode. On the other hand, to help you create an encoding declaration, the DOMDocument class is equipped with a method named createProcessingInstruction. Its syntax is:
Public Function createProcessingInstruction(ByVal target, ByVal data As String) As IXMLDOMProcessingInstruction
This method takes two arguments. The first argument can be passed as "xml". The second argument specifies the version of XML used. In most cases, this should be set to "version='1.0'". This method returns an object of type IXMLDOMProcessingInstruction. This means that you can call the DOMDocument.createProcessingInstruction() method and assign it to an IXMLDOMProcessingInstruction variable you would have previously declared. In reality, you have two versions. You can declare a variable of type IXMLDOMProcessingInstruction and use it. Here is an example:
Private Sub cmdCreateProcessingInstructions_Click() Dim docXMLDOM As DOMDocument Dim piDeclaration As IXMLDOMProcessingInstruction Set docXMLDOM = New DOMDocument Set piDeclaration = docXMLDOM.createProcessingInstruction("xml", "version='1.0'") Set docXMLDOM = Nothing End Sub
An alternative is to use a variable of type IXMLDOMNode.
Introduction to XML Elements
Elements Fundamentals
An XML document is made of small objects or sections named elements. Each element is created based on strict rules. A classic XML element is made of three parts:
There are rules you must follow when naming an element
The value can be anything, but some letters may not automatically display rightly. Such letters must be appropriatly formatted.
To support XML elements, the MSXML library is equipped with an interface named IXMLDOMElement. In the DOMNodeType enumeration, the XML element is represented by the NODE_ELEMENT member.
The Root Element
Every XML document must have one particular element that either is the only element in the document or acts as the parent of all the other elements of the same document. This element is referred to as the root. If there are many elements in an XML document, one of them must serve as the root. To give you access to the root element of an XML document, the DOMDocument class of the MSXML library has a property named documentElement. To specify that an element is the root, you can assign its reference to this property (we will see many examples). If an XML document exists already, to access its root, get a reference to the DOMDocument.documentElement property.
Fundamentals of Creating an XML Element
Introduction to Creating an XML Element
Since IXMLDOMElement is an interface, in order to use it, you must obtain its value from another class. To assist you with creating an element, the DOMDocument class is equipped with a method named createElement. Its syntax is:
Public Function createElement(ByVal tagName As String) As IXMLDOMElement
This method takes one argument and returns an element as a IXMLDOMElement object. When calling this method, you get a reference to the newly created element. If that element is the root, assign it to the documentElement property of the DOMDocument class. Here is an example:
Private Sub cmdCreateElement_Click()
Dim docXMLDOM As DOMDocument
Dim nodElement As IXMLDOMElement
Set docXMLDOM = New DOMDocument
Set nodElement = docXMLDOM.createElement(...)
Set docXMLDOM.documentElement = nodElement
Set docXMLDOM = Nothing
End Sub
The Name of an Element
As mentioned earlier, the primary formula of an element is a start tag, a name, and an end tag. The name of an element is the string that represents the tag. For example, in <Director>, the word Director is the name of the element. An element must have at least a start-tag. As mentioned in the previous section, you can create an element by calling the DOMDocument.createElement() method. This method takes one argument as the name of the element to be created. When calling this method, assign its returned value to an IXMLDOMNode-based variable, such as an IXMLDOMElement object, you would have declared. Here is an example:
Private Sub cmdCreateElement_Click()
Dim docXMLDOM As DOMDocument
Dim nodElement As IXMLDOMElement
Set docXMLDOM = New DOMDocument
Set nodElement = docXMLDOM.createElement("houses")
Set docXMLDOM.documentElement = nodElement
docXMLDOM.Save "C:\Exercises\Houses1.xml"
Set docXMLDOM = Nothing
End Sub
This would produce:
<?xml version="1.0"?> <house/>
If an element exists already, to let you get its name, the IXMLDOMNode interface is equipped with a property named tagName. Here is an example of accessing it:
Private Sub cmdCreateDOMDocument_Click()
Dim docXMLDOM As DOMDocument
Dim nodElement As IXMLDOMElement
Set docXMLDOM = New DOMDocument
Set nodElement = docXMLDOM.createElement("houses")
Set docXMLDOM.documentElement = nodElement
MsgBox nodElement.tagName
Set docXMLDOM = Nothing
End Sub
This would produce:
The Value of an Element
The value of an element is referred to as its text. To support such a value, the IXMLDOMNode interface is equipped with a String-based property named Text. To specify the name of an element, assign the desired string to its variable. Here is an example:
Private Sub cmdCreate_Click()
Dim docXMLDOM As DOMDocument
Dim nodElement As IXMLDOMElement
Set docXMLDOM = New DOMDocument
Set nodElement = docXMLDOM.createElement("house")
nodElement.Text = "Single Family"
Set docXMLDOM.documentElement = nodElement
docXMLDOM.Save "C:\Exercises\Houses2.xml"
Set docXMLDOM = Nothing
End Sub
This would produce:
<?xml version="1.0"?> <house>Single Family</house>
If an element exists already, to find out about its value, you can get its Text value.
The Values of an XML Document
To let you get all the values of an XML document, the IXMLDOMNode interface is equipped with a property named xml. Here is an example of accessing it:
Private Sub cmdCreate_Click() Dim docXMLDOM As DOMDocument Set docXMLDOM = New DOMDocument docXMLDOM.Load "C:\Exercises\Videos.xml" MsgBox docXMLDOM.xml Set docXMLDOM = Nothing End Sub
If an element exists already, to find out about its value, you can get its Text value.
Character Entities in an Element Value
Besides the obvious types of values, you may want to display special characters as values of elements. Consider the following example:
<FullName>Sylvie <Bellie> Aronson</FullName>
If you try using this XML document, for example, if you try displaying it in a browser, you would receive an error. The reason is that when the parser reaches the <FullName>Sylvie <Bellie> Aronson</FullName> line, it thinks that <Bellie> is a tag but then <Bellie> is not closed. The parser concludes that the document is not well-formed, that there is an error. For this reason, to display a special symbol as part of a value, you can use its character code. For example, the < (less than) character is represented with < and the > (greater than) symbol can be used with >. Therefore, the above code can be corrected as follows:
<FullName>Sylvie <Bellie> Aronson</FullName>
Here is a list of other codes you can use for special characters:
Code | Symbol | Code | Symbol | Code | Symbol | Code | Symbol | Code | Symbol |
' | ' | C | C | j | j | ³ | ³ | Ú | Ú |
< | < | D | D | k | k | ´ | ´ | Û | Û |
> | > | E | E | l | l | µ | µ | Ü | Ü |
& | & | F | F | m | m | ¶ | ¶ | Ý | Ý |
" | " | G | G | n | n | · | · | Þ | Þ |
! | ! | H | H | o | o | ¸ | ¸ | ß | ß |
" | " | I | I | p | p | ¹ | ¹ | à | à |
# | # | J | J | q | q | º | º | á | á |
$ | $ | K | K | r | r | » | » | â | â |
% | % | L | L | s | s | ¼ | ¼ | ã | ã |
& | & | M | M | t | t | ½ | ½ | ä | ä |
' | ' | N | N | u | u | ¾ | ¾ | å | å |
( | ( | O | O | v | v | ¿ | ¿ | æ | æ |
) | ) | P | P | w | w | À | À | ç | ç |
* | * | Q | Q | x | x | Á | Á | è | è |
+ | + | R | R | y | y | Â | Â | é | é |
, | , | S | S | z | z | Ã | Ã | ê | ê |
- | - | T | T | { | { | Ä | Ä | ë | ë |
. | . | U | U | } | } | Å | Å | ì | ì |
/ | / | V | V | ~ | ~ | Æ | Æ | í | í |
0 | 0 | W | W |   | empty | Ç | Ç | î | î |
1 | 1 | X | X | ¡ | ¡ | È | È | ï | ï |
2 | 2 | Y | Y | ¢ | ¢ | É | É | ð | ð |
3 | 3 | Z | Z | £ | £ | Ê | Ê | ñ | ñ |
4 | 4 | [ | [ | ¤ | ¤ | Ë | Ë | ò | ò |
5 | 5 | \ | \ | ¥ | ¥ | Ì | Ì | ó | ó |
6 | 6 | ] | ] | ¦ | ¦ | Í | Í | ô | ô |
7 | 7 | ^ | ^ | § | § | Î | Î | õ | õ |
8 | 8 | _ | _ | ¨ | ¨ | Ï | Ï | ö | ö |
9 | 9 | ` | ` | © | © | Ð | Ð | ÷ | ÷ |
: | : | a | a | ª | ª | Ñ | Ñ | ø | ø |
; | ; | b | b | « | « | Ò | Ò | ù | ù |
< | < | c | c | ¬ | ¬ | Ó | Ó | ú | ú |
= | = | d | d | ­ | | Ô | Ô | û | û |
> | > | e | e | ® | ® | Õ | Õ | ü | ü |
? | ? | f | f | ¯ | ¯ | Ö | Ö | ý | ý |
@ | @ | g | g | ° | ° | × | × | þ | þ |
A | A | h | h | ± | ± | Ø | Ø | ÿ | ÿ |
B | B | i | i | ² | ² | Ù | Ù | Ā | Ā |
There are still other codes to include special characters in an XML file.
Empty Elements
An element may not have a value but only a name. Consider the following example:
<video></video>
In this case, the video element doesn't have a value. It is called an empty element. When a tag is empty, the Text property of its IXMLDOMNode object would return an empty value.
Nesting an Element
As mentioned already, every XML document must have a primary element called the root. That root can exist by itself in the document, but if the document contains more than one element, the other elements must be created inside the root element. Such elements are created outside and on the right side of the start tag of the root but before the end tag. Here are examples of elements named FullName, PhoneNumber, and EmailAddress created inside a root element named Customer:
<?xml version="1.0" encoding="UTF-8"?> <Customer> <FullName></FullName> <PhoneNumbers></PhoneNumbers> <EmailAddress></EmailAddress> </Customer>
When an element B has been created inside another element A, Element B is said to be nested inside of Element A. We can also say that Element A is nesting Element B. In the same way, various combinations of nesting elements can be created. Here are examples:
<?xml version="1.0" encoding="UTF-8"?> <Customer> <FullName> <FirstName></FirstName> <LastName></LastName> </FullName> <PhoneNumbers> <HomePhone></HomePhone> <CellPhone></CellPhone> <WorkPhone></WorkPhone> <FaxNumber></FaxNumber> </PhoneNumbers> <EmailAddress></EmailAddress> </Customer>
The Child Nodes of a Node
Introduction
As mentioned already, one node can be nested inside of another. A nested node is called a child of the nesting node. This also implies that a node can have as many children as necessary, making them child nodes of the parent node. Consider our videos.xml example:
<?xml version="1.0" encoding="utf-8"?> <videos> <video> <title>The Distinguished Gentleman</title> <director>Jonathan Lynn</director> <rating>R</rating> </video> <video> <title>Chalte Chalte</title> <director>Aziz Mirza</director> <length>145 Mins</length> <format>DVD</format> </video> </videos>
The title and the director nodes are children of the video node. The video node is the parent of both the title and the director nodes.
To support the child nodes of a particular node, the IXMLDOMNode interface is equipped with a property named childNodes. To define this property, the MSXML library has an interface named IXMLDOMNodeList.
As you may realize, the childNodes property is a collection. This means that every member of the childNodes is an object of type IXMLDOMNode. This also means that if you have group of nested elements of a nesting node, those child elements, that are of type IXMLDOMElement, are in fact members of a collection of child nodes of their parent.
As you may know already, a collection uses an index that propvides a location to each of its members. Therefore, if you have a collection of child nodes, the first element has a position of 0, the second member uses an index as 1, and so on. Using this index, you can locate an element and perform any desired operation.
The number of child nodes in a collection is represented by a property named length, which of course is a number. You can use it to find the number of child nodes that an element has.
Accessing a Node in a Collection
The children of a node, that is, the members of a childNodes property, or the members of an IXMLDOMNodeList collection, can be located each by an index. The first node has an index of 0, the second has an index of 1, an so on. To give you access to a node of the collection, the IXMLDOMNodeList interface is equipped with an indexed property named Item. As an example, if a node has three children, to access the third, you can apply an index of 2 to its indexed property. Here is an example:
Private Sub cmdCreate_Click() Dim docXMLDOM As DOMDocument Set docXMLDOM = New DOMDocument docXMLDOM.loadXML "<?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>Chalte Chalte</Title>" & _ " <Director>Aziz Mirza</Director>" & _ " <Format>DVD</Format>" & _ " </Video>" & _ " <Video>" & _ " <Title>Her Alibi</Title>" & _ " <Rating>PG-13</Rating>" & _ " </Video>" & _ "</Videos>" MsgBox "Number of Main Nodes: " & CStr(docXMLDOM.childNodes.length) MsgBox "The root element has " & CStr(docXMLDOM.childNodes(1).childNodes.length) & " child nodes" Set docXMLDOM = Nothing End Sub
This would produce:
Remember that each child node is of type IXMLDOMNode, which in turn has a childNodes. This means that every time you access a child node, you get its own child nodes, if it has any. Based on this, you can call the childNodes collection of any element after accessing it by its index. Here are examples of how this can be done:
Private Sub cmdCreate_Click() Dim docXMLDOM As DOMDocument Set docXMLDOM = New DOMDocument docXMLDOM.loadXML "<?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>Chalte Chalte</Title>" & _ " <Director>Aziz Mirza</Director>" & _ " <Format>DVD</Format>" & _ " </Video>" & _ " <Video>" & _ " <Title>Her Alibi</Title>" & _ " <Rating>PG-13</Rating>" & _ " </Video>" & _ "</Videos>" MsgBox "The root element has " & CStr(docXMLDOM.childNodes(1).childNodes.length) & " child nodes." MsgBox "The first child node of the root element has " & CStr(docXMLDOM.childNodes(1).childNodes(0).childNodes.length) & " child nodes." MsgBox "The second child node of the root element has " & CStr(docXMLDOM.childNodes(1).childNodes(1).childNodes.length) & " child nodes." MsgBox "The third child node of the root element has " & CStr(docXMLDOM.childNodes(1).childNodes(2).childNodes.length) & " child nodes." Set docXMLDOM = Nothing End Sub
This would produce:
To better manage and manipulate the nodes of a collection of nodes, you must be able to access the desired node. The IXMLDOMNode interface combined with the IXMLDOMNodeList interface provide various means of getting to a node and taking the appropriate actions.
The Parent of a Node
Not all elements have child nodes. To indicate whether an element has child nodes, the IXMLDOMNode interface has a Boolean property named hasChildNodes.
If a node is a child, to let you get its parent, the IXMLDOMNode interface has a property named parentNode, which is of type IXMLDOMNode.
The First Child Node
The child nodes of a nesting element are recognized by their index in the collection. As mentioned already, the first child node has an index of 0. For a whole XML document that has a declaration, the first child of the DOM is:
<?xml version="1.0" encoding="utf-8"?>
After identifying or locating a node, the first node that immediately follows it is referred to as its first child. To let you access the first node of an element, the IXMLDOMNode interface is equipped with a property named firstChild, which is of type IXMLDOMNode. Based on the position of any element, if it has child nodes, you can find out what the first child is. Here is an example:
Private Sub cmdCreate_Click() Dim docXMLDOM As DOMDocument Set docXMLDOM = New DOMDocument docXMLDOM.loadXML "<?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>" & _ " <Director>Aziz Mirza</Director>" & _ " <Title>Chalte Chalte</Title>" & _ " <Format>DVD</Format>" & _ " </Video>" & _ " <Video>" & _ " <Rating>PG-13</Rating>" & _ " <Title>Her Alibi</Title>" & _ " </Video>" & _ "</Videos>" MsgBox "The first element from the root has the following values: " & _ docXMLDOM.childNodes(1).firstChild.Text Set docXMLDOM = Nothing End Sub
This would produce:
Consider the following XML document saved as videos1.xml:
<?xml version="1.0" encoding="utf-8" ?>
<Videos>
<Video>
<Title>The Distinguished Gentleman</Title>
<Director>Jonathan Lynn</Director>
<CastMembers>
<Actor>Eddie Murphy</Actor>
<Actor>Lane Smith</Actor>
<Actor>Sheryl Lee Ralph</Actor>
<Actor>Joe Don Baker</Actor>
<Actor>Victoria Rowell</Actor>
</CastMembers>
<Length>112 Minutes</Length>
<Format>DVD</Format>
<Rating>R</Rating>
</Video>
<Video>
<Title>Her Alibi</Title>
<Director>Bruce Beresford</Director>
<Length>94 Mins</Length>
<Format>DVD</Format>
<Rating>PG-13</Rating>
</Video>
<Video>
<Title>Chalte Chalte</Title>
<Director>Aziz Mirza</Director>
<Length>145 Mins</Length>
<Format>DVD</Format>
<Rating>N/R</Rating>
</Video>
</Videos>
Remember that every child node that is a member of a IXMLDOMNodeList collection is an object of type IXMLDOMNode and may have its own child node(s). This means that you can further get the ChildNodes collection of any node. Here is an example:
Private Sub cmdCreate_Click() Dim docXMLDOM As DOMDocument Set docXMLDOM = New DOMDocument docXMLDOM.Load "C:\Exercises\videos1.xml" MsgBox "The first element from the root has the following values: " & docXMLDOM.childNodes(1).firstChild.Text MsgBox "The first child node of the first element from the root has the following value: " & docXMLDOM.childNodes(1).childNodes(0).firstChild.Text MsgBox "The first child node of the second element from the root has the following value: " & docXMLDOM.childNodes(1).childNodes(1).firstChild.Text MsgBox "The first child node of the third element from the root has the following value: " & docXMLDOM.childNodes(1).childNodes(2).firstChild.Text Set docXMLDOM = Nothing End Sub
This would produce:
The Last Child Node
As opposed to the first child, the child node that immediately precedes the end-tag of the parent node is called the last child. To let you get the last child of a node, the IXMLDOMNode interface is equipped with a property named lastChild, which is of type IXMLDOMNode. Here is an example of accessing the last child node of the root element:
Private Sub cmdLastNode_Click()
Dim xmlDocument As Object
Set xmlDocument = CreateObject("MSXML2.DOMDocument.3.0")
xmlDocument.Load "C:\Exercises\videos1.xml"
MsgBox xmlDocument.XML
MsgBox "The last child node of the root element is:" & vbCrLf & _
xmlDocument.childnodes(1).lastchild.XML, _
vbOKOnly Or vbInformation, _
"Extensible Markup Language (XML)"
Set xmlDocument = Nothing
End Sub
This would produce:
The Next Sibling of a Node
The child nodes that are nested in a parent node and share the same level are referred to as siblings. If an elment has more than one child node, a node N that comes after a node M is referred to as its next sibling (Node M is next to Node N). To let you access the next sibling of a node, the IXMLDOMNode interface is equipped with a property named nextSibling property, which is of type IXMLDOMNode. Here is an example of accessing a next sibling:
Private Sub cmdSiblingNode_Click() Dim xmlDocument As Object Dim nodSecondNode As Object Set xmlDocument = CreateObject("MSXML2.DOMDocument.3.0") xmlDocument.Load "C:\Exercises\videos1.xml" Set nodSecondNode = xmlDocument.childnodes(1).childnodes(1) MsgBox "The second child node of the root element is:" & vbCrLf & _ nodSecondNode.XML, _ vbOKOnly Or vbInformation, _ "Extensible Markup Language (XML)" MsgBox "The sibling child node next to the second from the root element is:" & vbCrLf & _ nodSecondNode.NextSibling.XML, _ vbOKOnly Or vbInformation, _ "Extensible Markup Language (XML)" Set xmlDocument = Nothing End Sub
This would produce:
The Previous Sibling of a Node
Depending on its position, if one child node precedes another child node, the first child node is said to be the previous sibling of the other node. To let you access the previous sibling of a node, the IXMLDOMNode interface is equipped with a property named previousSibling. It is an IXMLDOMNode type of object. Here is an example of getting the previous sibling to a node:
Private Sub cmdSiblingNode_Click() Dim xmlDocument As Object Dim nodSecondNode As Object Set xmlDocument = CreateObject("MSXML2.DOMDocument.3.0") xmlDocument.Load "C:\Exercises\videos1.xml" Set nodSecondNode = xmlDocument.childnodes(1).childnodes(1) MsgBox "The sibling child node next to the second from the root element is:" & vbCrLf & _ nodSecondNode.PreviousSibling.XML, _ vbOKOnly Or vbInformation, _ "Extensible Markup Language (XML)" Set xmlDocument = Nothing End Sub
This would produce:
|
||
Previous | Copyright © 2000-2022, FunctionX, Inc. | Next |
|