Logo of Vovisoft

www.vovisoft.com

XML and The Dynamic Contents Approach - Part I

Nowaday, XML technology is growing rapidly because of its capabilities. Well, I am not going to name them all its advantages rather, I would like to concentrate on showing you how to build a dynamic contents using XML acts as a database like. Er!!! what do I mean by that? here is one scenario that we could look at...

Say you are developing a website for an online news company say cnn.com for instance. As you know, this website update frequently sometimes more than once/twice a day...yup, the articles are changing and the contents need to be updated regularly. However, the good thing is the presentation of the website pretty much remains the same. By the way, your boss also would like to keep all the old articles for future reference as well.

From the requirements above, a number of tasks that we must perform, those are:

  1. Design presentation of website and specify the hot spot for dynamic contents to be filled in,
  2. Build an XML database to store contents,
  3. Developing a scripting language to manipulate contents from XML database.

Design webpage and specify a hot spot

For example, we have designed the welcome page for our website as shown below:

From the design above, we expect to display the three latest articles (from our XML database). Each article, we will show:

  • the TITLE of the article,
  • SUMMARY of the article, and
  • provide a LINK to allow users to click and see the rest of the article.

If you are still unclear, click here to view the expected output.

Build XML database

Okay, you have seen the end result of that you wish to get, it certainly does help a lot in designing the database, right!.

For sure, what we want is a simple XML database but do the job. We can design skeleton of our XML database (articles.xml) like so:

<?xml version="1.0"?>
<articles>
	<article id="..." pubdate="...">
		<title>...</title>
		<story>...</story>
		<summary>...</summary>
	</article>
<articles>

Click here to view an XML database with some records...Of course, you might want to have a different database design to suit your requirements.

Build the script to pull out the data and display them in the specified hot spot

As you have seen the end result above, the three latest articles that extracted from XML database are shown properly. The best way to achieve it is simply write an ASP script (script.asp) to manipulate the three records out from XML database.

<%
	dim alen, i, atitle, asummary, artid
	set xmlDoc=CreateObject("Microsoft.XMLDOM")
	xmlDoc.async="false"
	
	' Open articles.xml database
	xmlDoc.load Server.MapPath("articles.xml")
	
	' Get title, summary tags and id attribute
	set atitle=xmlDoc.getElementsByTagName("title")
	set asummary=xmlDoc.getElementsByTagName("summary")
	set artid=xmlDoc.getElementsByTagName("article/@id")
	alen = atitle.length - 2
	
	' Print out the three latest articles only...
	for i = alen to atitle.length
		response.write(atitle.item(i-1).text)
		response.write(asummary.item(i-1).text)
		response.write("<a target='_new' 
				href='process.asp?mode=viewdetail&id=") & _
				artid.item(i-1).text & "'>" & _
				"More...</a>"
	next
%>

Once we have the script working fine, we now can embed this script - by referencing - into the template welcome.asp above.

<!-- #include file="script.asp" -->

Important: Make sure embed the line above at the specified hot spot, or else the result might not look like we have expected.

Download the source code!

See XML and the Dynamic Content Approach - Part II

Send Comments to Author or visit vovisoft forum.


Copyright © Vovisoft October 2001-2002