Credit: Jon A. Morse (ST Sci/NASA)
While XML is a new product, it works very well with
HTML and its related products, such as JavaScript and
CSS. In particular, XML can exist in what are called "XML Data
Islands" inside HTML. We will introduce the process of converting
XML to HTML by starting with
examples that emphasize the familiar features of HTML and its brethren
while progressively unveiling more features that manipulate XML.
Note that HTML and JavaScript processing of XML files requires not
only that the XML be well formed, but that it be valid as well
(XML writer is a $40 product that
validates XML files as well as processes XSL.
There are also free XML validators on the Web
that are designed for occasional use, such as:
XML Checker
and
STG Validator
which validates XML files on your desktop.
You could also use "google" and search for the phrase "validate
XML" for other examples).
Using a Restricted Form of XML
The first set of examples use an XML structure that is very common and
widely used, and which maps easily into HTML. It is called a "tabular"
or rectangular data set (in the four examples that follow, we use
an XML data set named booklist.xml).
The restriction is that the
portions of the tree structure we use are all at the same level
and have the same number of elements
Using the Full Unrestricted XML Format
The following examples are fully general and
can process any XML data file of any type or complexity. The
key ingredient for most of them
is a recursive function that "walks" through the
tree so it can visit any element node or attribute. The basic recursive
program, written in JavaScript, is only about 10 lines long, but may be
conceptually difficult to understand simply because it is recursive.
It is important to note that an XML document has a tree structure
above and beyond the structure of the data elements themselves. The
overall structure of an XML document is something like this:
Document |
|-- Processing Instruction
| (e.g., <?xml version="1.0">
|
|-- Comment
| (e.g., <!-- student DB file -->
|
|-- DTD
| (e.g., <!DOCTYPE BOOK SYSTEM "books.dtd">
|
|-- XML data file itself ...
|
The actual XML file is but a branch of a tree that usually
contains two or three other branches. Let "XMLdoc" be the name of
an XML document. Then the root of the actual XML file is given
by:
theRoot = XMLdoc.documentElement;
Note: in the examples that follow, you may use any of these XML files
for test purposes:
- Example 5: Display All Nodes in an XML File
This example simply
visits each element node in a tree and prints out its
value.
- Example 6: Display All Elements of a Certain Name
This scans through the tree, using a simple non-recursive
function, and displays
the
value of all elements of a
certain name.
- Example 7: Search for Any Element That Matches a String
This scans through the tree and locates the element (actually the
parentNode) that
contains
part of the search string
- Example 8: Search for Specific Elements that Match a String
This uses a non-recursive technique to locate those
elements
of a certain name
that match a string.
- Example 9: Search for Any Element or Attribute that Matches
This example uses a full recursive scan to search for all
all elements
OR attributes
that match an input string.
- Example 10: Edit an XML File
This example scans any XML file to
add,
remove, or modify element nodes
in a tree that match a certain string. All nodes that match a string
are subject to removal or modification, but addition of a node
only takes place immediately before the node that matches.
| |