Home

Other Types of XML Nodes

Fundamentals of Attributes

Introduction

As we have seen so far, XML elements are the objects of an XML document. We also know that one element can be nested inside of another element. Instead of nesting an element, you can make the nested element be part of the nesting element. This is the basis of an attribute. An attribute is an element created inside the start tag of an element. There are similarities and differences between an element and an attribute.

The element and the attribute have these in common:

The differences between an element and an attribute are:

Manually Creating an Attribute

As mentioned already, an attribute is created in the start tag of an element. To do it, in that tag and after the name of the element, type a name for the attribute. This can be done as follows:

<book ISBN></book>

The name of an attribute follows the same rules we defined for elements. An attribute should have a value. To specify the name of an attribute, assign a value as a string to its name. Here is an example:

<book ISBN="978-0-321-51294-9"></book>

Now, ISBN is an attribute of the video element. We already know how to populate an XML document with the desired elements.

We know that every element must be closed. We saw that we could close an element with an end-tag as follows:

<video><ISBN>0-7888-1623-3</ISBN></video>

We also saw that we could close an element locally as follows: <video />. If you create an attribute in an empty element, you can also close it by typing the indicative forward slash before the right angle bracket and after an empty space. Here is an example:

<video ISBN="0-7888-1623-3" />

When creating the elements, you can add an attribute to any element you want and omit it on any other element. There is no strict rule as to what element can/must have an attribute. It depends on how you decide to structure your document. Here are examples:

<?xml version="1.0" encoding="utf-8" ?>
</Books>
    <Book ISBN="978-0-13-611848-0" ShelfNumber="ECN-2048-69">
	<Title>Contemporary Engineering Economics - Fifth Edition</Title>
	<Authors>
	    <Author>Chan S. Park</Author>
	</Authors>
	<Copyright>
	    <Company>Pearson Education, Inc.</Company>
	    <YearPublished>2011</YearPublished>
	    <City>Upper Saddle River</City>
	    <State ZIPCode="07458">New Jersey</State>
	</Copyright>
    </book>
</books>

While a certain element may have an attribute, a sibling element with the same name may not have an attribute or may have a completely different type of attribute. Here is an XML file with attributes in some elements:

<?xml version="1.0" encoding="utf-8" ?>
<videos>
    <video ISBN="0-7888-1623-3">
	<title Screenplay="Marty Kaplan">The Distinguished Gentleman</title>
	<director>Jonathan Lynn</director>
	<length>112 Minutes</length>
	<format>DVD</format>
	<rating>R</rating>
    </video>
    <video>
	<title WrittenBy="Charlie Peter">Her Alibi</title>
	<director>Bruce Beresford</director>
	<length>94 Mins</length>
	<format>DVD</format>
	<rating>PG-13</rating>
    </video>
</videos>

Remember that you can include white spaces to make your code easy to read. This means that you can type an attribute on the next line of its element's name.

Operations on an XML Attribute

Creating an Attribute

B, before creating an attribute, you must first have an element. This can be the root node or another element. To let you create an attribute, the IXMLDOMNode interface provides a method named setAttribute. Its syntax is:

Public Sub setAttribute(ByVal name As String, value)

This method takes the name of the attribute and its value as arguments. Here is an example of creating an attribute:

Private Sub cmdCreateAttribute_Click()
    Dim docXMLDOM  As DOMDocument
    Dim nodBooks As IXMLDOMElement
    
    Set docXMLDOM = New DOMDocument
    
    ' Create the root node
    Set nodBooks = docXMLDOM.createElement("books")
    ' Create an attribute with a name and a value
    nodBooks.setAttribute "title", "Book Collection"

    Set docXMLDOM.documentElement = nodBooks
    ' Save the file
    docXMLDOM.Save "C:\Exercises\books1.xml"
    
    Set docXMLDOM = Nothing
End Sub

This would produce:

<?xml version="1.0"?>
<books title="Book Collection"></books>

With the same method, you can add as many attributes as you want. Here are examples:

Private Sub cmdCreateAttribute_Click()
    Dim docXMLDOM  As DOMDocument
    Dim nodBooks As IXMLDOMElement
    
    Set docXMLDOM = New DOMDocument
    
    ' Create the root node
    Set nodBooks = docXMLDOM.createElement("books")
    ' Create an attribute with a name and a value
    nodBooks.setAttribute "title", "Book Collection"
    ' Create other attributes
    nodBooks.setAttribute "goal", "Personal, education, and business"
    nodBooks.setAttribute "contact", "(108) 180-4086"
    ' Specify the root node
    Set docXMLDOM.documentElement = nodBooks
    ' Save the file
    docXMLDOM.Save "C:\Exercises\books2.xml"
    
    Set docXMLDOM = Nothing
End Sub

This would produce:

<?xml version="1.0"?>
<books title="Book Collection" contact="(108) 180-4086" goal="Personal, education, and business"/>


To make the document easy to read, it is usually a good idea to show each attribute on its own line. Here is an example:

<?xml version="1.0"?>
<books title="Book Collection"
       contact="(108) 180-4086"
       goal="Personal, education, and business"/>

In the same way, you can add attributes to the elements of your choice. Here are examples:

Private Sub cmdCreateAttributes_Click()
    Dim docXMLDOM  As DOMDocument
    Dim nodBook As IXMLDOMElement
    Dim nodBooks As IXMLDOMElement
    Dim nodChild As IXMLDOMElement
    Dim nodElement As IXMLDOMElement
    
    Set docXMLDOM = New DOMDocument
    
    ' Create the root node
    Set nodBooks = docXMLDOM.createElement("books")
    ' Add attributes to the root element
    nodBooks.setAttribute "title", "Book Collection"
    nodBooks.setAttribute "goal", "Personal, education, and business"
    nodBooks.setAttribute "contact", "(108) 180-4086"
    ' Specify the root node
    Set docXMLDOM.documentElement = nodBooks
    
    ' Start a new element
    Set nodBook = docXMLDOM.createElement("book")
    nodBook.setAttribute "website", "www.pearsonhighered.com"
    nodBook.setAttribute "Level", "Graduate"
    nodBook.setAttribute "ISBN-10", "0-13-611848-8"
    nodBook.setAttribute "ISBN-13", "978-0-13-611848-0"
    nodBooks.appendChild nodBook
    Set nodElement = docXMLDOM.createElement("title")
    nodElement.Text = "Contemporary Engineering Economics - Fifth Edition"
    nodBook.appendChild nodElement
    Set nodElement = docXMLDOM.createElement("authors")
    nodBook.appendChild nodElement
    Set nodChild = docXMLDOM.createElement("author")
    nodChild.Text = "Chan S. Park"
    nodChild.setAttribute "Employment", "Department of Industrial and Systems Engineering - Auburn University"
    nodElement.appendChild nodChild
    Set nodElement = docXMLDOM.createElement("copyright")
    Set nodChild = docXMLDOM.createElement("year")
    nodChild.Text = "2011"
    nodChild.setAttribute "years", "2002, 2007, 2011"
    nodElement.appendChild nodChild
    Set nodChild = docXMLDOM.createElement("company")
    nodChild.Text = "Pearson Education, Inc."
    nodElement.appendChild nodChild
    Set nodChild = docXMLDOM.createElement("city")
    nodChild.Text = "Upper Saddle River"
    nodElement.appendChild nodChild
    Set nodChild = docXMLDOM.createElement("state")
    nodChild.Text = "New Jersey"
    nodChild.setAttribute "ZIPCode", "07458"
    nodElement.appendChild nodChild
    nodBook.appendChild nodElement
    
    ' Start a new element
    Set nodBook = docXMLDOM.createElement("book")
    Set nodElement = docXMLDOM.createElement("title")
    nodElement.Text = "Introduction to Computer Security"
    nodBook.appendChild nodElement
    Set nodElement = docXMLDOM.createElement("authors")
    nodBook.appendChild nodElement
    Set nodChild = docXMLDOM.createElement("author")
    nodChild.Text = "Michael T. Goodrich"
    nodElement.appendChild nodChild
    Set nodChild = docXMLDOM.createElement("author")
    nodChild.Text = "Roberto Tamassia"
    nodElement.appendChild nodChild
    nodBooks.appendChild nodBook
    
    ' Save the file
    docXMLDOM.Save "C:\Exercises\books3.xml"
    
    Set docXMLDOM = Nothing
End Sub

This would produce:

<?xml version="1.0"?>
<books title="Book Collection" contact="(108) 180-4086" goal="Personal, education, and business">
  <book ISBN-13="978-0-13-611848-0" ISBN-10="0-13-611848-8" Level="Graduate" website="www.pearsonhighered.com">
    <title>Contemporary Engineering Economics - Fifth Edition</title>
    <authors>
      <author Employment="Department of Industrial and Systems Engineering - Auburn University">Chan S. Park</author>
    </authors>
    <copyright>
      <year years="2002, 2007, 2011">2011</year>
      <company>Pearson Education, Inc.</company>
      <city>Upper Saddle River</city>
      <state ZIPCode="07458">New Jersey</state>
    </copyright>
  </book>
  <book>
    <title>Introduction to Computer Security</title>
    <authors>
      <author>Michael T. Goodrich</author>
      <author>Roberto Tamassia</author>
    </authors>
  </book>
</books>

So far, we created the attributes at the same time we created the new document. Consider the following file named books4.xml:

<?xml version="1.0"?>
<books title="Book Collection" contact="(108) 180-4086" goal="Personal, education, and business">
  <book ISBN-13="978-0-13-611848-0" ISBN-10="0-13-611848-8" Level="Graduate" website="www.pearsonhighered.com">
    <title>Contemporary Engineering Economics - Fifth Edition</title>
    <authors>
	<author Employment="Department of Industrial and Systems Engineering - Auburn University">Chan S. Park</author>
    </authors>
    <copyright>
	<year years="2002, 2007, 2011">2011</year>
	<company>Pearson Education, Inc.</company>
	<city>Upper Saddle River</city>
	<state ZIPCode="07458">New Jersey</state>
    </copyright>
  </book>
  <book>
    <title>Finite Mathematics For Business, Economics, Life Sciences, and Social Sciences - Ninth Edition</title>
    <authors>
	<author Employment="Meritt College">Raymond A. Barnett</author>
	<author>Michael R. Ziegler</author>
	<author Employment="Marquette University">Karl E. Byleen</author>
    </authors>
  </book>
  <book>
    <title>Introduction to Computer Security</title>
    <authors>
      <author>Michael T. Goodrich</author>
      <author>Roberto Tamassia</author>
    </authors>
  </book>
</books>

You can open an existing XML file, locate a node and add an attribute to it. Here is an example that adds a new attribute to the title of second BOOK node:

Private Sub cmdCreateAttribute_Click()
    Dim docXMLDOM  As DOMDocument
    Dim lstBooks As IXMLDOMNodeList
    Dim nodElement As IXMLDOMElement
    
    ' Initialize the XML document model
    Set docXMLDOM = New DOMDocument
    ' Open the XML file
    docXMLDOM.Load "C:\Exercises\books4.xml"
    ' Get a list of all nodes named BOOK
    Set lstBooks = docXMLDOM.getElementsByTagName("title")
    
    ' Visit each node named BOOK
    For Each nodElement In lstBooks
        ' Look for a book with the following title
        If nodElement.Text = "Finite Mathematics For Business, Economics, Life Sciences, and Social Sciences - Ninth Edition" Then
            nodElement.setAttribute "ISBN-13", "978-0321-51294-9"

            ' Save the file
            docXMLDOM.Save "C:\Exercises\books4.xml"
            Exit For
        End If
    Next
    
    Set docXMLDOM = Nothing
End Sub

This would produce:

<?xml version="1.0"?>
<books title="Book Collection" goal="Personal, education, and business" contact="(108) 180-4086">
  <book website="www.pearsonhighered.com" Level="Graduate" ISBN-10="0-13-611848-8" ISBN-13="978-0-13-611848-0">
    <title>Contemporary Engineering Economics - Fifth Edition</title>
    <authors>
      <author Employment="Department of Industrial and Systems Engineering - Auburn University">Chan S. Park</author>
    </authors>
    <copyright>
      <year years="2002, 2007, 2011">2011</year>
      <company>Pearson Education, Inc.</company>
      <city>Upper Saddle River</city>
      <state ZIPCode="07458">New Jersey</state>
    </copyright>
  </book>
  <book>
    <title ISBN-13="978-0321-51294-9">Finite Mathematics For Business, Economics, Life Sciences, and Social Sciences - Ninth Edition</title>
    <authors>
      <author Employment="Meritt College">Raymond A. Barnett</author>
      <author>Michael R. Ziegler</author>
      <author Employment="Marquette University">Karl E. Byleen</author>
    </authors>
  </book>
  <book>
    <title>Introduction to Computer Security</title>
    <authors>
      <author>Michael T. Goodrich</author>
      <author>Roberto Tamassia</author>
    </authors>
  </book>
</books>

In the same way, you can add an attribute to any element you can locate.

A Collection of Attributes

An element can have 0, one, or more attributes. The attributes of an element are stored in an indexed property named attributes. This property is a member of the IXMLDOMNode interface. The IXMLDOMNode.attributes property is based on an interface named IXMLDOMNamedNodeMap. Inside of an element that has attributes, each attribute occupies a specific position, which is its index within the attributes property. You can use that index to access an attribute.  Here is an example that accesses the attributes, if any, of all elements named book:

Private Sub cmdAttributeCollection_Click()
    Dim i As Integer
    Dim docXMLDOM  As DOMDocument
    Dim lstBooks As IXMLDOMNodeList
    Dim nodElement As IXMLDOMElement
    
    ' Initialize the XML document model
    Set docXMLDOM = New DOMDocument
    ' Open the XML file
    docXMLDOM.Load "C:\Exercises\books4.xml"
    ' Get a list of all nodes named BOOK
    Set lstBooks = docXMLDOM.getElementsByTagName("book")

    ' Visit each node named BOOK
    For Each nodElement In lstBooks
        For i = 0 To nodElement.Attributes.length - 1
            MsgBox nodElement.Attributes(i).Text
        Next
    Next
    Set docXMLDOM = Nothing
End Sub

Once you have accessed an attribute, you can perform the necessary operation on it.

An Attribute Node

To support XML attributes, the MSXML library provides an interface named IXMLDOMAttribute. Like all nodes, this interface is derived from IXMLDOMNode. You can use that interface to build an attribute. To support the idea of creating an attrubute, the DOMDocument interface is equipped with a method named createAttribute. Itssyntax is:

Public Function createAttribute(ByVal name As String) As IXMLDOMAttribute

This method takes as argument the name of the attribute you want to create. After calling this method, to specify its value, assign a string to its Text proterty its inherits from the IXMLDOMNode interface. After creating the attribute, to let you add it to its parent element, the IXMLDOMNode interface is equipped with a method named setAttributeNode. Its syntax is:

Public Function setAttributeNode(ByVal attribute As IXMLDOMAttribute) As IXMLDOMAttribute

This method takes as argument the new attribute object you want to create. Normally, this argument would result from calling the DOMDocument.createAttribute() method. Before adding an attribute, you should first identify its parent element. Here is an example that adds an attribute to the root element:

Private Sub cmdAddAttribute_Click()
    Dim docXMLDOM  As DOMDocument
    Dim lstBooks As IXMLDOMNodeList
    Dim nodElement As IXMLDOMElement
    Dim atrAuthor As IXMLDOMAttribute
    
    ' Initialize the XML document model
    Set docXMLDOM = New DOMDocument
    ' Open the XML file
    docXMLDOM.Load "C:\Exercises\books4.xml"
    ' Get a list of all nodes named author
    Set lstBooks = docXMLDOM.getElementsByTagName("author")
    
    ' Visit each node named author
    For Each nodElement In lstBooks
        ' Look for a book with the following title
        If nodElement.Text = "Michael R. Ziegler" Then
            ' Create a new attribute
            Set atrAuthor = docXMLDOM.createAttribute("institution")
            ' Specify the value of the new attribute
            atrAuthor.Text = "Marquette University"
            ' Add the new attribute to the element
            nodElement.setAttributeNode atrAuthor

            ' Save the file
            docXMLDOM.Save "C:\Exercises\books4.xml"
            Exit For
        End If
    Next
    
    Set docXMLDOM = Nothing
End Sub

This would produce:

<?xml version="1.0"?>
<books title="Book Collection" goal="Personal, education, and business" contact="(108) 180-4086">
  <book website="www.pearsonhighered.com" Level="Graduate" ISBN-10="0-13-611848-8" ISBN-13="978-0-13-611848-0">
    <title>Contemporary Engineering Economics - Fifth Edition</title>
    <authors>
      <author Employment="Department of Industrial and Systems Engineering - Auburn University">Chan S. Park</author>
    </authors>
    <copyright>
      <year years="2002, 2007, 2011">2011</year>
      <company>Pearson Education, Inc.</company>
      <city>Upper Saddle River</city>
      <state ZIPCode="07458">New Jersey</state>
    </copyright>
  </book>
  <book>
    <title ISBN-13="978-0321-51294-9">Finite Mathematics For Business, Economics, Life Sciences, and Social Sciences - Ninth Edition</title>
    <authors>
      <author Employment="Meritt College">Raymond A. Barnett</author>
      <author institution="Marquette University">Michael R. Ziegler</author>
      <author Employment="Marquette University">Karl E. Byleen</author>
    </authors>
  </book>
  <book>
    <title>Introduction to Computer Security</title>
    <authors>
      <author>Michael T. Goodrich</author>
      <author>Roberto Tamassia</author>
    </authors>
  </book>
</books>

Attribute Removal

If an element has an attribute you don't want or that you don't need anymore, you can delete that attribute. To let you delete an attribute, the IXMLDOMElement interface is equipped with a method named removeAttribute. Its syntax is:

Public Sub removeAttribute(ByVal name)

When calling this method, pass the name of the attribute you want to delete. Consider the following file named videos6.xml:

<?xml version="1.0"?>
<books title="Book Collection" contact="(108) 180-4086" goal="Personal, education, and business">
  <book ISBN13="978-0-13-611848-0" ISBN10="0-13-611848-8" Level="Graduate" website="www.pearsonhighered.com">
    <title>Contemporary Engineering Economics - Fifth Edition</title>
    <authors>
	<author Employment="Department of Industrial and Systems Engineering - Auburn University">Chan S. Park</author>
    </authors>
    <copyright>
	<year years="2002, 2007, 2011">2011</year>
	<company>Pearson Education, Inc.</company>
	<city>Upper Saddle River</city>
	<state ZIPCode="07458">New Jersey</state>
    </copyright>
  </book>
  <book>
    <title>Finite Mathematics For Business, Economics, Life Sciences, and Social Sciences - Ninth Edition</title>
    <authors>
	<author initials="RAB" employment="Meritt College">Raymond A. Barnett</author>
	<author initials="MRZ" institution="Marquette University">Michael R. Ziegler</author>
	<author initials="KEB" employment="Marquette University">Karl E. Byleen</author>
    </authors>
  </book>
  <book ISBN13="978-0-321-51294-9" ISBN10="0-321-51294-4">
    <title>Introduction to Computer Security</title>
    <authors>
      <author>Michael T. Goodrich</author>
      <author>Roberto Tamassia</author>
    </authors>
  </book>
</books>

Here is an example of removing an attribute:

Private Sub cmdRemoveAttribute_Click()
    Dim docXMLDOM  As DOMDocument
    Dim lstBooks As IXMLDOMNodeList
    Dim nodElement As IXMLDOMElement
    
    ' Initialize the XML document model
    Set docXMLDOM = New DOMDocument
    ' Open the XML file
    docXMLDOM.Load "C:\Exercises\books6.xml"
    ' Get a list of all nodes named author
    Set lstBooks = docXMLDOM.getElementsByTagName("author")
    
    ' Visit each node named author
    For Each nodElement In lstBooks
        ' Look for the following author
        If nodElement.Text = "Karl E. Byleen" Then
            
            ' If you find it, delete its attribute named initials
            nodElement.removeAttribute "initials"

            ' Save the file
            docXMLDOM.Save "C:\Exercises\books6.xml"
            Exit For
        End If
    Next
    
    Set docXMLDOM = Nothing
End Sub

This would produce:

<?xml version="1.0"?>
<books title="Book Collection" contact="(108) 180-4086" goal="Personal, education, and business">
  <book ISBN13="978-0-13-611848-0" ISBN10="0-13-611848-8" Level="Graduate" website="www.pearsonhighered.com">
    <title>Contemporary Engineering Economics - Fifth Edition</title>
    <authors>
	<author Employment="Department of Industrial and Systems Engineering - Auburn University">Chan S. Park</author>
    </authors>
    <copyright>
	<year years="2002, 2007, 2011">2011</year>
	<company>Pearson Education, Inc.</company>
	<city>Upper Saddle River</city>
	<state ZIPCode="07458">New Jersey</state>
    </copyright>
  </book>
  <book>
    <title>Finite Mathematics For Business, Economics, Life Sciences, and Social Sciences - Ninth Edition</title>
    <authors>
	<author initials="RAB" employment="Meritt College">Raymond A. Barnett</author>
	<author initials="MRZ" institution="Marquette University">Michael R. Ziegler</author>
	<author employment="Marquette University">Karl E. Byleen</author>
    </authors>
  </book>
  <book ISBN13="978-0-321-51294-9" ISBN10="0-321-51294-4">
    <title>Introduction to Computer Security</title>
    <authors>
      <author>Michael T. Goodrich</author>
      <author>Roberto Tamassia</author>
    </authors>
  </book>
</books>

Another technique you can use consists of defining an IXMLDOMAttribute object and submitting to its IXMLDOMElement parent to delete. To let you do this,the IXMLDOMElement interface is equipped with a method named removeAttributeNode. Its syntax is:

Public Function removeAttributeNode(ByVal oldAttr As IXMLDOMAttribute) As IXMLDOMAttribute

When calling this method, pass the attribute object as argument. If the attribute exists, it would be removed and the method would return the deleted attribute. Here is an example:

Private Sub cmdRemoveAttribute_Click()
    Dim i As Integer
    Dim docXMLDOM  As DOMDocument
    Dim lstBooks As IXMLDOMNodeList
    Dim nodElement As IXMLDOMElement
    Dim atrISBN10 As IXMLDOMAttribute
    
    ' Initialize the XML document model
    Set docXMLDOM = New DOMDocument
    ' Open the XML file
    docXMLDOM.Load "C:\Exercises\books6.xml"
    ' Get a list of all nodes named author
    Set lstBooks = docXMLDOM.getElementsByTagName("book")
    
    ' Visit each node named author
    For Each nodElement In lstBooks
        For i = 0 To nodElement.Attributes.length - 1
            ' Look for the following author
            If nodElement.Attributes(i).Text = "0-321-51294-4" Then
            
                Set atrISBN10 = docXMLDOM.createAttribute("ISBN10")
                nodElement.removeAttributeNode nodElement.Attributes(i)

                ' Save the file
                docXMLDOM.Save "C:\Exercises\books6.xml"
            Exit For
            End If
        Next
    Next
    
    Set docXMLDOM = Nothing
End Sub

This would produce:

<?xml version="1.0"?>
<books title="Book Collection" contact="(108) 180-4086" goal="Personal, education, and business">
  <book ISBN13="978-0-13-611848-0" ISBN10="0-13-611848-8" Level="Graduate" website="www.pearsonhighered.com">
    <title>Contemporary Engineering Economics - Fifth Edition</title>
    <authors>
	<author Employment="Department of Industrial and Systems Engineering - Auburn University">Chan S. Park</author>
    </authors>
    <copyright>
	<year years="2002, 2007, 2011">2011</year>
	<company>Pearson Education, Inc.</company>
	<city>Upper Saddle River</city>
	<state ZIPCode="07458">New Jersey</state>
    </copyright>
  </book>
  <book>
    <title>Finite Mathematics For Business, Economics, Life Sciences, and Social Sciences - Ninth Edition</title>
    <authors>
	<author initials="RAB" employment="Meritt College">Raymond A. Barnett</author>
	<author initials="MRZ" institution="Marquette University">Michael R. Ziegler</author>
	<author employment="Marquette University">Karl E. Byleen</author>
    </authors>
  </book>
  <book ISBN13="978-0-321-51294-9">
    <title>Introduction to Computer Security</title>
    <authors>
      <author>Michael T. Goodrich</author>
      <author>Roberto Tamassia</author>
    </authors>
  </book>
</books>

When calling the IXMLDOMElement.removeAttributeNode() method, if the attribute doesn't exist, nothing would happen.

Comments and Data Sections

Introduction

To differentiate the various nodes that belong to an XML file, they are classified by their category. As mentioned earlier, the types of node are listed in the DOMNodeType enumerator.

Comments

A comment is a character, a line or a paragraph that is not considered as part of the XML code that needs to be processed. A comment allows you to insert notes or personal observations inside an XML document. For this reason, a commented section can be written any way you like. This means that a comment can include plain text, formulas, expressions, or even XML code as long as you know that that XML code will not be validated: it will ignored by the parser.

To manually create a comment, you use the following formula:

<!-- Blah Blah Blah ->

Between <!-- and -->, any text in that section is considered a comment and you can include anything you want. Here an example:

<?xml version="1.0" encoding="utf-8"?>
<!-- In this collection, we will keep each title "as is" -->
<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 Mins</length>
	<format>DVD</format>
	<rating>PG-13</rating>
  </video>
</videos>

To support comments, the MSXML library has an interface named IXMLDOMComment. To let you create a comment, the DOMDocument classs is equipped with a method named createComment. Its syntax is:

public virtual XmlComment CreateComment(string data);

This method takes as argument the text that would go into the commented section. After calling it, if the method succeeds, which it usually does, it returns the IXMLDOMComment object. After calling the method to create the comment, to actually add the comment to the document, call the DOMDocument.appendChild() method. Here is an example:

Private Sub cmdCreateComment_Click()
    Dim docXMLDOM  As DOMDocument
    Dim Comment As IXMLDOMComment
    
    ' Initialize the XML document model
    Set docXMLDOM = New DOMDocument
    ' Open the XML file
    docXMLDOM.Load "C:\Exercises\books6.xml"
    
    Set Comment = docXMLDOM.createComment("This is my personal book collection.")
    docXMLDOM.appendChild Comment
    
    ' Save the file
    docXMLDOM.Save "C:\Exercises\books6.xml"
  
    Set docXMLDOM = Nothing
End Sub

This would produce:

<?xml version="1.0"?>
<books title="Book Collection" goal="Personal, education, and business" contact="(108) 180-4086">
<!--This is my personal book collection.-->
  <book website="www.pearsonhighered.com" Level="Graduate" ISBN10="0-13-611848-8" ISBN13="978-0-13-611848-0">
    <title>Contemporary Engineering Economics - Fifth Edition</title>
    <authors>
      <author Employment="Department of Industrial and Systems Engineering - Auburn University">Chan S. Park</author>
    </authors>
    <copyright>
      <year years="2002, 2007, 2011">2011</year>
      <company>Pearson Education, Inc.</company>
      <city>Upper Saddle River</city>
      <state ZIPCode="07458">New Jersey</state>
    </copyright>
  </book>
  <book>
    <title>Finite Mathematics For Business, Economics, Life Sciences, and Social Sciences - Ninth Edition</title>
    <authors>
      <author employment="Meritt College" initials="RAB">Raymond A. Barnett</author>
      <author initials="MRZ" institution="Marquette University">Michael R. Ziegler</author>
      <author employment="Marquette University" initials="KEB">Karl E. Byleen</author>
    </authors>
  </book>
  <book ISBN10="0-321-51294-4" ISBN13="978-0-321-51294-9">
    <title>Introduction to Computer Security</title>
    <authors>
      <author>Michael T. Goodrich</author><author>Roberto Tamassia</author>
    </authors>
  </book>
</books>

You can also locate an element and add a comment in its section. Here is an example:

Private Sub cmdCreateComments_Click()
    Dim docXMLDOM  As DOMDocument
    Dim Comment As IXMLDOMComment
    Dim lstBooks As IXMLDOMNodeList
    Dim nodElement As IXMLDOMElement
    
    ' Initialize the XML document model
    Set docXMLDOM = New DOMDocument
    ' Open the XML file
    docXMLDOM.Load "C:\Exercises\books6.xml"
    ' Get a list of all nodes named author
    Set lstBooks = docXMLDOM.getElementsByTagName("title")
    
    Set Comment = docXMLDOM.createComment("This is my personal book collection.")
    docXMLDOM.appendChild Comment
    
    ' Visit each node named author
    For Each nodElement In lstBooks
        ' Look for a book with the following title
        If nodElement.Text = "Introduction to Computer Security" Then
            ' Create a new comment
            Set Comment = docXMLDOM.createComment("This is one of my main books on computer security.")
            nodElement.appendChild Comment
           ' Save the file
            docXMLDOM.Save "C:\Exercises\books6.xml"
            Exit For
        End If
    Next
    
    Set docXMLDOM = Nothing
End Sub

This would produce:

<?xml version="1.0"?>
<books title="Book Collection" goal="Personal, education, and business" contact="(108) 180-4086">
<!--This is my personal book collection.-->
  <book website="www.pearsonhighered.com" Level="Graduate" ISBN10="0-13-611848-8" ISBN13="978-0-13-611848-0">
    <title>Contemporary Engineering Economics - Fifth Edition</title>
    <authors>
      <author Employment="Department of Industrial and Systems Engineering - Auburn University">Chan S. Park</author>
    </authors>
    <copyright>
      <year years="2002, 2007, 2011">2011</year>
      <company>Pearson Education, Inc.</company>
      <city>Upper Saddle River</city>
      <state ZIPCode="07458">New Jersey</state>
    </copyright>
  </book>
  <book>
    <title>Finite Mathematics For Business, Economics, Life Sciences, and Social Sciences - Ninth Edition</title>
    <authors>
      <author employment="Meritt College" initials="RAB">Raymond A. Barnett</author>
      <author initials="MRZ" institution="Marquette University">Michael R. Ziegler</author>
      <author employment="Marquette University" initials="KEB">Karl E. Byleen</author>
    </authors>
  </book>
  <book ISBN10="0-321-51294-4" ISBN13="978-0-321-51294-9">
    <title>Introduction to Computer Security
      <!--This is one of my main books on computer security.-->
    </title>
    <authors>
      <author>Michael T. Goodrich</author><author>Roberto Tamassia</author>
    </authors>
  </book>
</books>

CDATA

Except for comments, the parser is used to "scan" the whole XML file to analyze it. Every tag is then interpreted. As we mentioned already, the value of each tag can be displayed in a browser between its opening and its closing tag, and the browser uses different font styles to make a distinction. When creating some tags and some sections of the file, you may want the parser to consider those particular tags and sections as regular text. That is, you may want the parser to treat a certain tag and its value as if it were regular text even though it is created as an XML file.

To prevent the parser from interpreting a tag regularly but to treat that tag and its value as regular text, you can create a section named CDATA. Start the section with <![CDATA[, followed by anything you want, and ending with ]]>. The formula used is:

<![CDATA[ Blah Blah Blah ]]>

Between <![CDATA[ and ]]>, you can type anything, including one or more normal XML tags. To support CDATA section, the MSXML library provides an interface named IXMLDOMCDATASection. To let you programmatically start creating a CDATA section, the DOMDocument class is equipped with a method named createCDataSection() method. Its syntax is:

Public Function createCDataSection(ByVal data As String) As IXMLDOMCDATASection

This method receives the content of the CDATA section as argument. If the method succeeds, which it usually does, it returns an IXMLDOMCDATASection value. Therefore, after calling the DOMDocument.createCDataSection() method, get its return value and assign it to the root node. Here is an example:

Private Sub cmdCDATASection_Click()
    Dim docXMLDOM  As DOMDocument
    Dim nodRoot As IXMLDOMElement
    Dim CDATASection As IXMLDOMCDATASection
    
    ' Initialize the XML document model
    Set docXMLDOM = New DOMDocument
    ' Open the XML file
    docXMLDOM.Load "C:\Exercises\books6.xml"
    
    Set nodRoot = docXMLDOM.documentElement
    
    Set CDATASection = docXMLDOM.createCDATASection( _
        "<books>" & _
        "  <book ISBN="""">" & _
        "    <title subtitle=""If any""></title>" & _
        "    <authors>" & _
        "      <author></author>" & _
        "    </authors>" & _
        "    <copyright>" & _
        "      <year></year>" & _
        "      <company></company>" & _
        "      <city></city>" & _
        "      <state ZIPCode=""If any""></state>" & _
        "    </copyright>" & _
        "    <cover></cover>" & _
        "  </book>" & _
        "</books>")
    nodRoot.appendChild CDATASection
    
    ' Save the file
    docXMLDOM.Save "C:\Exercises\books6.xml"
End Sub

This would produce:

<?xml version="1.0"?>
<books title="Book Collection" goal="Personal, education, and business" contact="(108) 180-4086">
<!--This is my personal book collection.-->
  <book website="www.pearsonhighered.com" Level="Graduate" ISBN10="0-13-611848-8" ISBN13="978-0-13-611848-0">
    <title>Contemporary Engineering Economics - Fifth Edition</title>
    <authors>
      <author Employment="Department of Industrial and Systems Engineering - Auburn University">Chan S. Park</author>
    </authors>
    <copyright>
      <year years="2002, 2007, 2011">2011</year>
      <company>Pearson Education, Inc.</company>
      <city>Upper Saddle River</city>
      <state ZIPCode="07458">New Jersey</state>
    </copyright>
  </book>
  <book>
    <title>Finite Mathematics For Business, Economics, Life Sciences, and Social Sciences - Ninth Edition</title>
    <authors>
      <author employment="Meritt College" initials="RAB">Raymond A. Barnett</author>
      <author initials="MRZ" institution="Marquette University">Michael R. Ziegler</author>
      <author employment="Marquette University" initials="KEB">Karl E. Byleen</author>
    </authors>
  </book>
  <book ISBN10="0-321-51294-4" ISBN13="978-0-321-51294-9">
    <title>Introduction to Computer Security
      <!--This is one of my main books on computer security.-->
    </title>
    <authors>
      <author>Michael T. Goodrich</author><author>Roberto Tamassia</author>
    </authors>
  </book>
  <books>
  <![CDATA[</books>
             <book ISBN="">" & _
               <title subtitle="If any"></title>" & _
               <authors>" & _
                 <author></author>" & _
               </authors>" & _
               <copyright>" & _
                 <year></year>" & _
                 <company></company>" & _
                 <city></city>" & _
                 <state ZIPCode="If any"></state>" & _
               </copyright>" & _
               <cover></cover>" & _
             </book>" & _
           </books>]]>
</books>

Previous Copyright © 2000-2022, FunctionX, Inc. Next