Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
Paul Litwin is a senior consultant with Litwin Consulting and
MCW Technologies focusing on application development employing ASP,
Access, Visual Basic, Visual InterDev, SQL Server, and related
technologies. Paul is the conference chair for ASP Connections, VB
Connections, and XML Connections conferences. He has written
several books including the Access 2000 Developer's Handbook,
Volume I: Desktop Edition, Access 2000 Developer's Handbook, Volume
II: Enterprise Edition, Intranet & Web Databases for Dummies,
and VBA for Dummies Quick Reference. Paul also trains developers
for AppDev and speaks at a variety of U.S. and international
conferences. You can reach Paul at plitwin@mcwtech.com.
The paper's author, Paul Litwin, teaches ASP and other classes
for Application Developers Training Company. For more information
on training classes offered by Application Developers Training
Company, visit www.appdev.com, or, in the U.S., call
800-578-2062.
XML stands for Extensible Markup Language. XML isn't so much a
language as it is a system for defining other languages-languages
about data. The W3C defines XML as "a common syntax for expressing
structure in data." We like to think of XML as "ASCII on
steroids."
Both HTML and XML have a common ancestor; they are both
derivatives of Standard Generalized Markup Language (SGML), a
language for how to specify a document markup language or tag set.
But while HTML has a fixed set of tags, XML, like SGML, lets you
define your own tags.
What XML Is
XML is
a W3C standard
a tag-based markup language
a metalanguage-a language to define other languages
a way to express data
case-sensitive
self describing
readable by humans
a great way to share and transfer information from one
program/system/process to another
What XML Isn't
XML is not
a replacement for HTML
concerned with displaying information
owned by any vendor
yet universally supported by Web browsers
a database
So Why Do I Care About XML?
XML lets you separate data from presentation. Using XML you can
create rich, self-describing data documents that are easily
transferred from one place to another. XML is a standard for
exchanging structured data between components, applications and
systems.
TIP: Because XML is extensible it
can be targeted at specific uses. There are standardized XML
definitions for financial data, mathematics in the Mathematical
Markup Language (MathML), Multimedia (SMIL), Graphics in the Vector
Markup Language (VML), Channels in the Channel Definition Format
(CDF), Hypertext (XHTML), as well as lower-level standards like,
resource linking (XPointer and XLink), and style sheets (XSL).
Working with XML
Let's begin with a simple XML example.
An XML Example
Here's the XML source for a simple XML document that describes a
person's laptop computer (mycomputer.xml):
<?xml version="1.0"?>
<mycomputer>
<pc>
<type>Laptop</type>
<brand>Toshiba</brand>
<model>Tecra
8000</model>
<processor>300 Mhz Pentium
II</processor>
<ram>128 MB</ram>
<drives>
<drive>9
MB Hard Disk</drive>
<drive>1.4 MB Floppy Disk</drive>
<drive>CD-ROM</drive>
</drives>
<display>14 inch active
matrix LCD panel</display>
<modem>Toshiba internal
V.90</modem>
<network>Xircom Cardbus
Ethernet II 10/100</network>
</pc>
<docking_station/>
<case>
<brand>USL</brand>
<color>black</color>
<fabric>vinyl</fabric>
</case>
</mycomputer>
You'll notice a few things about this XML document:
It's all plain text.
Unlike HTML, the tags are all made up. The tags are used to
describe the data.
As you can see, XML supports nested hierarchical data.
Everything is there in the XML file. The document describes
itself.
The XML Declaration
Although it's optional, every XML file should begin with the XML
declaration:
<?xml version="1.0"?>
The XML declaration must be entered in lower case. The XML
declaration is part of the prolog of the XML document. In this
particular example, it's the only part of the prolog, but the
prolog may also contain processing instructions and document type
declarations (both of which are discussed later in the
chapter).
XML Elements
Like HTML documents, XML documents consist primarily of
elements. An element consists of a starting tag, content, and an
ending tag. Like HTML, XML uses the angle bracket characters as tag
delimiters. And like HTML, ending tags are the same as starting
tags with the addition of the "/" character. Unlike HTML, elements
in XML are case-sensitive.
For example, shown here is the ram element, with "128 MB" as its
content:
<ram>128 MB</ram>
All XML documents consist of a single root-level document
element (also referred to as the root element). The XML
specification forbids multiple document elements. All other
elements in the document must be descendants of the document
element.
In the mycomputer.xml example, "mycomputer" is the document
element, with all the elements descending from mycomputer:
<?xml version="1.0"?>
<mycomputer>
...
</mycomputer>
Element Names
Element names must begin with a letter, an underscore, or a
colon. Each character after the first may be a letter, a number, an
underscore, a colon, a hyphen, or a period. Element names may not
contain spaces.
Element Contents
Content-the text between the opening and closing element
tags-may not contain any of the following characters:
<
>
&
If you need to include these characters, you can use the
following entities instead:
Character
Entity Reference
<
<
>
>
&
&
For example:
<formula>x < y + 5</formula>
TIP: You can also define your own
entities using entity declarations. Entity declarations are part of
the document type definition (DTD).
Nesting of Elements
All child elements must reside completely within their parent
elements. The following nesting example is legal in XML:
<case>
<brand>USL</brand>
<color>black</color>
<fabric>vinyl</fabric>
</case>
The following nesting is not legal XML because the brand
element is a child element of case so its ending tag must reside
within the case element:
<case>
<brand>
<color>black</color>
<fabric>vinyl</fabric>
</case>
</brand>
Empty Elements
XML includes a shortcut for specifying an element with no
content using the empty-element tag:
<element/>
This empty-element tag is equivalent to:
<element></element>
In the mycomputer.xml example, the docking_station element is
represented using an empty-element tag:
<docking_station/>
A Second Example
The following XML file, mycomputer2.xml, contains the same data
as mycomputer.xml but some of the information is expressed as
attributes rather than content (see mycomputer2.xml):
Unlike HTML, you must delimit all attributes values in XML with
quotes.
So how do you decide whether to include some piece of data as
content or an attribute? There are no rules-it's totally up to
you.
TIP: It's usually better to use
elements to contain data and attributes to contain metadata (data
about data). You are more limited when storing data within
attributes. For example, you can nest elements within other
elements but you can't do the same with attributes. Also, it's more
difficult to manipulate attribute data programmatically than it is
to manipulate element data.
Comments
XML comments are of the form:
<!--comment text goes here -->
This is the same comment format used in HTML.
Comments are not considered part of the textual content of an
XML document and therefore XML processors are not required to pass
comments along to the application. This is similar to how HTML
comments are handled by Web browsers-the browser never displays
them.
Namespaces
Say that you've created an XML document that you wish to merge
with another XML document that was created by another author. A
problem arises when you've both used the same element names to mean
different things. The solution to the problem is to use
namespaces.
A namespace is a collection of element and/or attribute names
that are associated with a Uniform Resource Identifier (URI) to
make the names unique.
You declare a namespace using the xmlns attribute of an
element's starting tag. The namespace can then be referenced by
that element and any child elements. For example, the following XML
snippet (from mycomputer_ns1.xml) declares two namespaces (org and
pc) to be used by children of the pc element:
<?xml version="1.0"?>
<mycomputer>
<pc xmlns:org="urn:appdev.com:schema-org"
xmlns:pc="urn:appdev.com:schema-pc">
<org:type>Accounting</org:type>
<pc:type>Laptop</pc:type>
This allows you to use the organizational type element and the
computer type element within the same document.
You can declare a namespace to be the default namespace for all
elements within its scope by eliminating the prefix.
For example, in the following XML (from mycomputer_ns2.xml) we
have made the "appdev.com:schema-pc" namespace the default
namespace. Thus, the second type element falls under the
"appdev.com:schema-pc" namespace:
<?xml version="1.0"?>
<mycomputer>
<pc xmlns:org="urn:appdev.com:schema-org"
xmlns="urn:appdev.com:schema-pc">
<org:type>Accounting</org:type>
<type>Laptop</type>
These examples have used URN namespaces. You point the xmlns
attribute to either a unique URL or a unique URN. A Uniform
Resource Name (URN) is an alternate way to specify a unique
resource. In either case, the URL or URN does not have to exist.
It's merely used to make the namespace unique.
TIP: While the URL or URN doesn't
have to point to an actual resource, it might be useful if it did
point to a document that described the namespace.
Processing Instructions
Like comments, processing instructions are not part of the
textual content of an XML document. Unlike comments, however, XML
processors are required to pass processing instructions along to
the application. You use processing instructions to pass special
information along to the application that will be processing the
XML document.
Processing instructions follow this syntax:
<? name data?>
The XML declaration that appears at the top of XML documents is
an example of a processing instruction. Processing instructions
that begin with "xml" are reserved.
XML Browser Support
Currently (as of the Spring, 2000), only Internet Explorer
supports XML, XSL, and the XML DOM natively. Netscape Navigator 4.5
does not support XML, although you can use add-on XML parsers with
Netscape Navigator. The next version of Netscape-code-named
Mozilla-should support XML.
A Great XML Resource
A great resource for the XML developer is the Microsoft Windows
DNA XML Resource Kit. The resource kit is available as a CD that
Microsoft has distributed widely. To order a copy of the CD for
shipping and handling costs, go to:
The kit includes technical articles and code samples on XML,
XSL, Windows DNA, ADO, BizTalk, SOAP, and more. You'll also find
several XML utilities, including XML Notepad, an editor for
creating XML documents, DTD2Schema, a utility that converts DTDs
into XML-Data schemas, and the SQL Server XML Technology
Preview.
XML documents can include an optional Document Type Definition
(DTD) to ensure that the XML follows a specified format. DTDs use
an arcane SGML-based syntax that only an SGML-junkie could
love.
A DTD Example
For example, here's mycomputer3.xml with a DTD (see
mycomputer3.xml):
<display>14 inch active
matrix LCD panel</display>
<modem>Toshiba internal
V.90</modem>
<network>Xircom Cardbus
Ethernet II 10/100</network>
</pc>
<docking_station/>
<case brand="USL">
<color>black</color>
<fabric>vinyl</fabric>
</case>
</mycomputer>
You can also reference an external DTD from a document. For
example, here's the beginning of the mycomputer4.xml document that
references the mycomputer.dtd DTD:
<?xml version="1.0"?>
<!DOCTYPE mycomputer SYSTEM "mycomputer.dtd">
<mycomputer>
And here's the mycomputer.dtd file:
<!ELEMENT mycomputer (pc, docking_station, case)>
<!ELEMENT pc (processor, ram, drives, display, modem,
network)>
<!ATTLIST pc
type (Laptop|Desktop) "Desktop"
brand CDATA #IMPLIED
model CDATA #IMPLIED>
<!ELEMENT processor (#PCDATA)>
<!ELEMENT ram (#PCDATA)>
<!ELEMENT drives (drive*)>
<!ELEMENT drive (#PCDATA)>
<!ELEMENT display (#PCDATA)>
<!ELEMENT modem (#PCDATA)>
<!ELEMENT network (#PCDATA)>
<!ELEMENT docking_station (#PCDATA)>
<!ELEMENT case (color, fabric)>
<!ATTLIST case
brand CDATA #IMPLIED>
<!ELEMENT color (#PCDATA)>
<!ELEMENT fabric (#PCDATA)>
NOTE:A full discussion of DTDs is beyond the scope of this
course.
Valid and Well-Formed Documents
A well-formed XML document is an XML document that is
syntactically correct. To be well-formed a document must abide by a
number of rules for well-formedness. These include (but are not
limited to):
The document must consist of one or more elements.
The document must consist of exactly one document element with
all other elements properly nested within the document
element.
All elements must contain opening and closing tags.
A well-formed XML document that also conforms to its DTD is said
to be a valid XML document. XML parsers (also known as processors)
can validate a document against a DTD.
Not all XML parsers are validating processors. Internet Explorer
5 contains a validating XML parser, but you can turn validation
off.
XML-Data and Schema
DTDs have a number of problems, including:
You must learn a new language for creating DTDs.
DTDs lack rich data typing capabilities.
DTDs aren't extensible. You either meet the DTD or you
don't.
XML-Data was created to remedy the problems with DTDs. XML-Data
is an XML language you use to describe the schema of a document.
XML-Data is extensible, flexible, and powerful.
Here's the XML-Data schema equivalent of the mycomputer DTD:
NOTE:A full discussion of XML-Data schemas is beyond the scope
of this course.
Document Validity-Is It Necessary?
If your XML documents will have a long lifetime or you plan to
use XML for transferring data between businesses, then you may want
to validate your XML documents against a DTD or schema. However, if
you are using XML for passing data between different tiers of a
3-tier application, then you probably won't care about document
validity.
If you do need to produce a large number of valid XML documents,
you'll want investigate purchasing a DTD/Schema creation utility.
Another possibility is to investigate an XML DTD/Schema framework
such as Microsoft's BizTalk.
The XML Document Object Model (DOM) was created to allow
developers to be able to programmatically parse and manipulate the
objects in an XML document.
The XML DOM object hierarchy is simple-it merely mimics the
nesting of objects of your XML document. Figure 1 illustrates a
typical XML DOM object model: the document object and a number of
nested node objects.
Figure 1. An XML DOM hierarchy.
The XML DOM Objects
The XML DOM consists of four objects:
document - the XML document (not the document node)
node - a node in the XML document
nodeList - a list of sibling nodes; some DOM methods return a
nodelList object
parseError - an object that can be used to retrieve information
about the last parsing error
You work with an XML document by employing various methods and
properties of the four XML DOM objects from a scripting
language.
A DOM Example
The nodes1v.htm and nodes1j.htm pages retrieve the name of the
first child element of the mydocument.xml document element and
display it on the page. Yes, the result is less than awe-inspiring,
but this example demonstrates the basics of working with the XML
DOM.
These pages both use the mydocument.xml document. The source for
mycomputer.xml was shown earlier in the chapter. We've repeated it
here:
<?xml version="1.0"?>
<mycomputer>
<pc>
<type>Laptop</type>
<brand>Toshiba</brand>
<model>Tecra
8000</model>
<processor>300 Mhz Pentium
II</processor>
<ram>128 MB</ram>
<drives>
<drive>9
MB Hard Disk</drive>
<drive>1.4 MB Floppy Disk</drive>
<drive>CD-ROM</drive>
</drives>
<display>14 inch active
matrix LCD panel</display>
<modem>Toshiba internal
V.90</modem>
<network>Xircom Cardbus
Ethernet II 10/100</network>
</pc>
<docking_station/>
<case>
<brand>USL</brand>
<color>black</color>
<fabric>vinyl</fabric>
</case>
</mycomputer>
Here's the complete nodes1v.htm page. In this version of the
page, the script, all executed on the client side in Internet
Explorer, is written in VBScript (see nodes1v.htm):
<HTML>
<HEAD>
<TITLE>XML Example (nodes1v.htm)</TITLE>
<SCRIPT LANGUAGE=vbscript>
<!--
Function getXML()
Dim xmlDoc
Dim xmlRoot
Dim xmlNode
Set xmlDoc =
CreateObject("Microsoft.xmldom")
xmlDoc.async =
False
xmlDoc.load("mycomputer.xml")
Set xmlRoot =
xmlDoc.documentElement
Set xmlNode =
xmlRoot.childNodes(0)
divHere.innerHTML =
_
"<H3>"
& xmlNode.nodeName & "</H3>"
End Function
-->
</SCRIPT>
</HEAD>
<BODY onload="getXML()">
<H1>Mycomputer pc Node</H1>
<DIV ID="divHere"></DIV>
</BODY>
</HTML>
Here's the JavaScript version of the same page, nodes1j.htm.
There are some minor differences but the basic gist of the page is
the same as for the VBScript version of the page. After all, the
DOM is language neutral (see nodes1j.htm):
<HTML>
<HEAD>
<TITLE>XML Example (nodes1j.htm)</TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function getXML() {
var xmlDoc
var xmlRoot
var xmlNode
xmlDoc = new
ActiveXObject("Microsoft.xmldom")
xmlDoc.async =
false
xmlDoc.load("mycomputer.xml")
xmlRoot =
xmlDoc.documentElement
xmlNode =
xmlRoot.childNodes(0)
divHere.innerHTML
=
"<H3>" +
xmlNode.nodeName + "</H3>"
}
//-->
</SCRIPT>
</HEAD>
<BODY onload="getXML()">
<H1>Mycomputer pc Node</H1>
<DIV ID="divHere"></DIV>
</BODY>
</HTML>
The next few sections describe the various parts of the nodes1v
and nodes1j examples.
Loading an XML Document
Before you can work with it in a Web page, you must load the XML
document. And before you can load a document, you must instantiate
the XML DOM object.
The following code, from nodes1v.htm, illustrates how to
instantiate the XML DOM using the VBScript CreateObject method and
how to use the document object's load method to load the XML
document into the page:
Set xmlDoc =
CreateObject("Microsoft.xmldom")
xmlDoc.async =
False
xmlDoc.load("mycomputer.xml")
TIP: You can also use a ProgId of
"MSXML.DOMDocument" instead of "Microsoft.xmldom".
Notice that we have used the async method of the document object
to load the XML document synchronously. This causes the browser to
wait until the entire XML document is loaded before proceeding. If
you don't include this code, the browser will attempt to parse the
code before it is completely loaded.
TIP: The Load method loads an XML
document from a file. You can also use the LoadXML method to load
an XML document from a string.
Here's the equivalent code written in JavaScript from
nodes1j.htm. In JavaScript, you instantiate the DOM using the
ActiveXObject object. Again, we set the async property to false to
ensure that the entire document is loaded before proceeding:
xmlDoc = new
ActiveXObject("Microsoft.xmldom")
xmlDoc.async =
false
xmlDoc.load("mycomputer.xml")
The Document Element
The following code retrieves a reference to the document
element, the parent node of the XML document.
Here's the code in VBScript:
Set xmlRoot =
xmlDoc.documentElement
And here's the same code in JavaScript:
xmlRoot =
xmlDoc.documentElement
The ChildNodes Collection
Once you have a reference to the document node, you can use the
document node's childNodes collection to retrieve nodes.
The following code obtains a reference to the first node of the
document element's childNodes collection:
Set xmlNode =
xmlRoot.childNodes(0)
And here's the JavaScript version of the code:
xmlNode =
xmlRoot.childNodes(0)
TIP: All collections in XML start
numbering with 0. So the first element in a collection is always
item 0, the second element is item 1, and so forth.
Displaying the XML on the Page
Of course, you have to find some way to display the XML data on
the page or else all is for naught. One convenient mechanism is to
use the innerHTML property of an HTML tag to blast the XML right
onto the page. That's exactly what we do in nodes1v.htm:
divHere.innerHTML =
_
"<H3>"
& xmlNode.nodeName & "</H3>"
This code uses the nodeName property of the node to obtain the
name of the XML element.
The same code from nodes1j.htm:
divHere.innerHTML
=
"<H3>" +
xmlNode.nodeName + "</H3>"
Calling the DOM Scripting Code
The only thing left to do is to call the DOM code. Nodes1v.htm
and nodes1j.htm both call the getXML function from the load event
of the BODY tag:
<BODY onload="getXML()">
<H1>Mycomputer XML Example</H1>
<DIV ID="divHere"></DIV>
</BODY>
</HTML>
The nodes1v.htm page (which looks the same as the nodes1j.htm)
page is shown in IE 5 in Figure 2.
Figure 2. Not going to win any awards, but the "pc" on this page
came from the mydocument.xml XML document.
Iterating Through a Document's Nodes
The mydocument.xml document tree is illustrated in Figure 3.
Figure 3. An XML tree (or hierarchy) for mydocument.xml.
You can use the childNodes collection repeatedly to retrieve the
entire XML tree of an XML document. The nodes2v.htm page retrieves
the entire XML tree from the mycomputer.xml document and displays
it in a series of HTML tables.
Here's the code from nodes2v.htm used to iterate through the
first level of nodes (pc, docking_station, case) from
mycomputer.xml:
Set xmlRoot =
xmlDoc.documentElement
For Each xmlPNode In
xmlRoot.childNodes
strDoc = strDoc & _
"<H3>" & xmlPNode.nodeName & "</H3>"
' ...continued...
Next
A second loop is used to iterate through the second level of
nodes (type, brand, model, etc.) from mycomputer.xml:
If xmlPNode.childNodes.length = 0 Then
strDoc = strDoc & _
"<I>No data</I>"
Else
strDoc = strDoc & "<TABLE border>"
For Each xmlNode In xmlPNode.childNodes
strDoc = strDoc & "<TR>" & _
"<TD><B>" & xmlNode.nodeName & _
":</B></TD>" & _
"<TD>" & xmlNode.text + "</TD>" & _
"</TR>"
Next
End If
strDoc = strDoc & "</TABLE>"
This code first checks to see if the childNodes collection of
each xmlPNode node is empty before creating a table. If it is
empty, that is the childNodes.length property is 0, then the
"<I>No data</I>" string is output; otherwise the code
creates a table and uses a second For Each loop to iterate through
the child nodes. The code uses the node object's text property to
display the text of an element.
The resulting page is shown in Figure 4.
Here's the full source code of the nodes2v.htm page:
<HTML>
<HEAD>
<TITLE>XML Example (nodes2v.htm)</TITLE>
<STYLE>
H3 {color: blue}
</STYLE>
<SCRIPT LANGUAGE=vbscript>
<!--
Option Explicit
Function getXML()
Dim xmlDoc
Dim xmlRoot
Dim xmlPNode
Dim xmlNode
Dim strDoc
Set xmlDoc =
CreateObject("Microsoft.xmldom")
xmlDoc.async =
False
xmlDoc.load("mycomputer.xml")
Set xmlRoot =
xmlDoc.documentElement
For Each xmlPNode In
xmlRoot.childNodes
strDoc = strDoc & _
"<H3>" & xmlPNode.nodeName & "</H3>"
If xmlPNode.childNodes.length = 0 Then
strDoc = strDoc & _
"<I>No data</I>"
Else
strDoc = strDoc & "<TABLE border>"
For Each xmlNode In xmlPNode.childNodes
strDoc = strDoc & "<TR>" & _
"<TD><B>" & xmlNode.nodeName & _
":</B></TD>" & _
"<TD>" & xmlNode.text + "</TD>" & _
"</TR>"
Next
End If
strDoc = strDoc & "</TABLE>"
Next
divHere.innerHTML =
strDoc
End Function
-->
</SCRIPT>
</HEAD>
<BODY onload="getXML()">
<H1>Mycomputer XML Tree</H1>
<DIV ID="divHere"></DIV>
</BODY>
</HTML>
Figure 4. The output from the mycomputer2.htm page.
The JavaScript version of the page, nodes2j.htm uses two for
loops instead of the VBScript For Each loops to iterate through the
XML tree of mydocument.xml. Here's the complete looping code from
nodes2j.htm:
xmlRoot =
xmlDoc.documentElement
strDoc =
"<TABLE>"
for (intI=0;
intI <=
xmlRoot.childNodes.length-1;
intI++) {
xmlPNode = xmlRoot.childNodes(intI)
strDoc = strDoc +
"<H3>" + xmlPNode.nodeName + "</H3>"
if (xmlPNode.childNodes.length == 0)
strDoc = strDoc + "<I>No data</I>"
else {
strDoc = strDoc + "<TABLE border>"
for (intJ=0;
intJ <= xmlPNode.childNodes.length-1;
intJ++) {
xmlNode = xmlPNode.childNodes(intJ)
strDoc = strDoc + "<TR>" +
"<TD><B>" + xmlNode.nodeName +
":</B></TD>" +
"<TD>" + xmlNode.text + "</TD>" +
"</TR>"
}
strDoc = strDoc + "</TABLE>"
}
}
Retrieving a Particular Node or Set of Nodes
You don't always want to retrieve nodes in a loop. Sometimes, it
would be more convenient to just pluck a set of nodes from the XML
tree. This is what the document object's getElementsByTagName
method does.
Here's the syntax for getElementsByTagName:
nodelist = document.getElementsByTagName(tagname)
where
nodelist is a nodeList object,
document is a document object,
tagname is string identifying an element. For example,
"case/brand". Use "*" to retrieve the entire XML document
tree.
The getElementsByTagName method returns a nodeList object.
A nodeList object is essentially equivalent to a childNodes
collection. It has one property (length) and three methods (item,
nextNode, and reset). You use the item method of a nodeList object
to retrieve an item from the collection of nodes in the
nodeList.
Here's the scripting code from nodes3v.htm that takes advantage
of the getElementsByTagName method to retrieve the pc/ram element
text:
Function getXML()
Dim xmlDoc
Dim xmlNodes
Set xmlDoc =
CreateObject("Microsoft.xmldom")
xmlDoc.async =
False
xmlDoc.load("mycomputer.xml")
' Retrieve the
collection of nodes at pc/ram
Set xmlNodes =
xmlDoc.getElementsByTagName("pc/ram")
' And use the item
property to retrieve the
' text from the
pc/ram tag
divHere.innerHTML =
xmlNodes.item(0).text
End Function
And here's the equivalent code from the JavaScript version of
the page, nodes3j.htm:
function getXML() {
var xmlDoc
var xmlNodes
xmlDoc = new
ActiveXObject("Microsoft.xmldom")
xmlDoc.async =
false
xmlDoc.load("mycomputer.xml")
// Retrieve the
collection of nodes at pc/ram
xmlNodes =
xmlDoc.getElementsByTagName("pc/ram")
// And use the item
property to retrieve the
// text from the
pc/ram tag
divHere.innerHTML =
xmlNodes.item(0).text
}
Another useful method for retrieving a particular node is the
selectSingleNode method of the node object. Its syntax is shown
here:
Set snode = node.selectSingleNode(element_name)
For example, the following code retrieves the text from the
CustomerId descendant of the xmlNode node and sticks the text in
the strText variable:
Not all XML data is stored in the text of elements. Sometimes,
data is stored as attributes of elements. You can iterate through
the attributes of an element using the attributes collection.
The mycomputer2.xml file is similar to the mycomputer.xml file
with one exception. Some of the element data for the pc and case
nodes has been stored as attributes rather than as child nodes.
Here's the pc and case tags from mycomputer2.xml:
This VBScript code from attributes1v.htm iterates through the
xmlPCNode node's attributes collection, using the nodeName and text
properties (see attributes1v.htm):
For Each xmlAtt in
xmlPCNode.attributes
strDoc = strDoc & _
"<BR><B>" & xmlAtt.nodeName &
":</B>" & _
xmlAtt.text
Next
The equivalent JavaScript code to iterate through the xmlPCNode
node's attributes collection, from attributes1j.htm, is shown
here:
for (intI=0;
intI <=
xmlPCNode.attributes.length-1;
intI++) {
xmlAtt =
xmlPCNode.attributes(intI)
strDoc = strDoc +
"<BR><B>" + xmlAtt.nodeName + ": </B>"
+
xmlAtt.text
}
Attibutes1j.htm is shown in Figure 5.
Figure 5. The attributes of the pc node.
You can also retrieve the text for a single attribute using the
getAttribute method of a node object.
For example, this code, from attributes2v.htm, retrieves the
brand attribute of the case element:
Set xmlNodes = xmlDoc.getElementsByTagName("case")
This section has only scratched the surface of the XML DOM.
There are many more properties and methods defined by the XML DOM,
plus additional extended properties and methods defined by
Microsoft. The shear breadth of the XML DOM is staggering. In many
cases, there are numerous ways to accomplish a given task, with no
clear "right way" to do things.
The best way to learn the XML DOM is to experiment.
TIP: You might find it helpful to
set a reference to Microsoft XML (msxml.dll) from Visual Basic or
another Microsoft product that supports IntelliSense. Then, you can
use IntelliSense and the Microsoft Object Browser to browse the
objects, collections, properties, and methods of the XML DOM.
All of the examples in this chapter have performed the
processing of XML on the client, in Internet Explorer. You can also
process XML on the server by instantiating the XMLDOM component
from an ASP page.
An ASP Example
The nodes2v.asp page performs the same transformation that the
earlier nodes2v.htm page performed, but this time, all the
processing is done on the server by the ASP engine. Here's the
source code for nodes2v.asp:
<%@ Language=VBScript %>
<%Option Explicit%>
<HTML>
<HEAD>
<TITLE>XML/ASP Example (nodes2v.asp)</TITLE>
<STYLE>
H3 {color: blue}
</STYLE>
</HEAD>
<H1>Mycomputer XML Tree Parsed on the
Server</H1>
<%
Dim xmlDoc
Dim xmlRoot
Dim xmlPNode
Dim xmlNode
Dim strDoc
Set xmlDoc = Server.CreateObject("Microsoft.xmldom")
xmlDoc.async = False
xmlDoc.load(Server.MapPath("mycomputer.xml"))
Set xmlRoot = xmlDoc.documentElement
For Each xmlPNode In xmlRoot.childNodes
strDoc = strDoc &
_
"<H3>"
& xmlPNode.nodeName & "</H3>"
If
xmlPNode.childNodes.length = 0 Then
strDoc = strDoc & _
"<I>No data</I>"
Else
strDoc = strDoc & "<TABLE border>"
For Each xmlNode In xmlPNode.childNodes
strDoc = strDoc & "<TR>" & _
"<TD><B>" & xmlNode.nodeName &
":</B></TD>" & _
"<TD>" & xmlNode.text + "</TD>" & _
"</TR>"
Next
End If
strDoc = strDoc &
"</TABLE>"
Next
Response.Write strDoc
%>
</BODY>
</HTML>
Notice the use of the Server.MapPath method to convert the
virtual file path into a file path that the XML DOM can use.
The nodes2v.asp page is shown Figure 6 as seen in Netscape
Navigator 4.72-a version of the browser that lacks native support
for XML. This is, of course, possible because the XML is processed
entirely on the Web server by the ASP engine.
Figure 6. This page parses the XML tree from an ASP page,
nodes2v.asp.
TIP: ADO 2.5 supports the saving
of recordsets to files as XML. The current support is somewhat
limited but it's certain that future versions of ADO and ASP will
include more complete support for XML.
This paper has been adapted, with permission,
from:
The paper's author, Paul Litwin, teaches ASP and
other classes for Application Developers Training Company. For more
information on training classes offered by Application Developers
Training Company, visit www.appdev.com, or, in the U.S., call
800-578-2062.