In part one of this series, we discussed how to retrieve remote data from an XML file via JavaScript. In this article, we'll learn how to process that data in a more complex manner. As an example, we'll take groups of XML data, separate individual segments of that data and display those segments in different ways, depending on how they're identified.
This article builds on the example code used in the previous article, so if you don't understand the code we start with here, you may want to go back and read it.
Let's Begin
Let's begin with our first step: forming the XML. We want to write an XML document
that groups sets of data to be processed by JavaScript, so we'll group some
nodes and sub-nodes (or, elements and sub-elements) together. In the example,
we'll use the names of some household pets:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<pets>
<pet>Cat</pet>
<pet>Dog</pet>
<pet>Fish</pet>
</pets>
</data>
|
Above, we have the XML declaration (states that the document is an XML 1.0 document, encoded with the UTF-8 character set standard), a root element (
) that groups all underlying elements together, a element that groups all pets together, and then a node for each pet. To specify what type of animal each pet is, we put a text node within the elements: Cat, Dog, and Fish.