Lists from XML databases from predefined keywords




Fixed extractions from XML files

One of the good parts about working with databases is, that you only have to put in the data once, and then you can retrieve the data as you see fit. One of the extractions that you could benefit from, is a fixed extraction of a specific part of the database. It might be something like tables with all the Danish or German suppliers on a list of suppliers, or all the laptops on an inventory list. By choosing the structure of the XML database with a little care, this can be an immensely strong tool for the web site.

Using XML databases this is an extremely simple task.


The XML file

The XML file consists of a field for searching and a field for return value. It can easily be the same field, but due to the way we express ourself, using negations and synonyms, it is often a better approach to search in one field and return the content of another. If we try making a list of jokes as a model, it could look like this:

<?xml version="1.0" encoding="UTF-8"?>
<Jokes>

<Joke>
<Type>CrossingRoad</Type>
<Text><![CDATA[- Why did the chicken cross the road?<BR>
- To get to the other side!]]></Text>
</Joke>

<Joke>
<Type>LightBulbs</Type>
<Text><![CDATA[- How many real men does it take to change a light bulb?<BR>
- None, real men aren't afraid of the dark!]]></Text>
</Joke>

<Joke>
<Type>CrossingRoad</Type>
<Text><![CDATA[- Why did the elephant cross the road?<BR>
- It was the chicken's day off!]]></Text>
</Joke>

<Joke>
<Type>Lawyers</Type>
<Text><![CDATA[- What do you call 1000 lawyers at the bottom of the sea?<BR>
- A good start!]]></Text>
</Joke>

<Joke>
<Type>CrossingRoad</Type>
<Text><![CDATA[- Why did the chicken cross the road, roll in the mud, and go back again?<BR>
- Because it was a dirty double crosser!]]></Text>
</Joke>

</Jokes>

The list is called Jokes.xml, and is placed in the same directory as the HTML file, with the JavaScript pulling the list.


The JavaScript

When making a simpel list like this, the easiest and fastest way of doing it, is an internal script using document.write(). If you want to do something else, for some reason, you are of course more than welcome to do so.

The JavaScript consists of three parts:
  1. Accessing the XML file
  2. Parsing the Type fields in the XML filens
  3. If the Type field contains the search criteria, the content of the Joke field is returned and written, and a spacer is added.

In this example we will extract the jokes about why the chicken crossed the road, and the spacer is two breaks, a horizontal line, and a break, i.e. <BR><BR><HR><BR>.


The first thing we do is accessing the XML file. For this we use the JavaScript loadXMLDoc(). How this is done, can be read here. So far, the JavaScript now looks like this:

<SCRIPT TYPE="text/javascript">
xmlDoc=loadXMLDoc("Jokes.xml");

</SCRIPT>

Next thing is getting the values from the two tags Type and Text. This is done by using getElementsByTagName():

<SCRIPT TYPE="text/javascript">
xmlDoc=loadXMLDoc("Jokes.xml");

var Type_lmnt = xmlDoc.getElementsByTagName("Type");
var Joke_lmnt = xmlDoc.getElementsByTagName("Text");

</SCRIPT>

We now have the two tags and what we do is parsing Type_lmnt until we find the field value CrossingRoad. It looks like this:

<SCRIPT TYPE="text/javascript">
xmlDoc=loadXMLDoc("Jokes.xml");

var Type_lmnt = xmlDoc.getElementsByTagName("Type");
var Joke_lmnt = xmlDoc.getElementsByTagName("Text");

for (i = 0; i < Type_lmnt.length; i++)
if (Type_lmnt[i].firstChild.nodeValue == "CrossingRoad") { }

</SCRIPT>

When the script, during the parsing, encounter the field value CrossingRoad, we need it to write the content of the Text tag, i.e. Joke_lmnt, followed by <BR><HR><BR>. Then it looks like this:

<SCRIPT TYPE="text/javascript">
xmlDoc=loadXMLDoc("Jokes.xml");

var Type_lmnt = xmlDoc.getElementsByTagName("Type");
var Joke_lmnt = xmlDoc.getElementsByTagName("Text");

for (i = 0; i < Type_lmnt.length; i++)
if (Type_lmnt[i].firstChild.nodeValue == "CrossingRoad") {
document.write(Joke_lmnt[i].firstChild.nodeValue + "<BR><BR><HR><BR>");}

</SCRIPT>


On the screen, it looks like this:



Obviously, the if construction can be expanded with AND, OR, ELSE etc. as needed, and if you want it displayed e.g. in a table, as shown for simple lists here, you can do this as well.