ARTICLES

Home  > Articles  >  Objects in XML

 

               

About the Author: Dr.Vanitha Vaidialingam, PhD, IRS is a technology management consultant of repute.  She worked with the Government of India on some of the major national level egovernance projects.  She currently works with some of the leading corporate houses in India in setting up their technology frameworks and advising them on managing their established technology.  She has her own content rich information web portal www.homemakersalmanac.com. She can be contacted at smnthsai@grihast.com.

 

In the previous tutorial “Displaying XML data in HTML files” we focused upon dynamic html files and how data from an xml file can be displayed in an HTML file at run time. This tutorial helps you appreciate:

 

  1. what objects are and how they are related to the structure of the HTML file
  2. how to create hierarchical trees to represent HTML files
  3. what node and nodelist means and how they can be used to extract data from xml files
  4. how HTML files can be used to extract data from a single-level xml file and
  5. how to handle errors that may occur while reading data from xml files.

 

 

XML is not object oriented.  So what objects?

 

XML is not object oriented language in the sense that the elements of the xml vocabulary are not related to each other by inheritance.  They also do not correspond to the objects precisely. 

 

A class in an object oriented language defines a chunk of data with a list of fields and methods for accessing and manipulating the fields.  When one class extends another to add new fields and new methods, the original fields and methods are kept intact.  In xml the derivations do not happen that way. While structural similarities may exist, the fields and methods may vary considerably.

 

Secondly content models aren’t inherited in xml. Content models can be changed to suit business needs and these don’t generally fit into the inheritance design.

 

Antagonists would assert that xml is all about data while OO is all about hiding data.  XML stores data, exchanges data between unrelated applications and represents data within an application.  OO encapsulates data and operates upon data.  The same antagonists would strongly oppose combining OO and xml as they would assert that OO programming will kill the benefits that can be derived from xml.

However, OO is often combined with xml in data centric applications for a variety of reasons including the advantages of functional programming.  Moreover, there are some areas of common interest between object oriented programming and xml, which cannot be ignored. XML DTD and XML schemas also lean towards rigid structuring which is similar to OO programming.

 

The areas of common interest between xml and oo are listed in the table below:

 

Common areas of interest

Object oriented programming

Xml

Extensibility

Sub classing

New Tags

Interface

Method prototyping

Schemas

Modularity

Classes

Namespaces

Componentization

Classes

Structural types

Self description

RT Class Info, inspection

Tag Names

 

 

 

 

The hierarchical tree that represented the structure of the document in the email message in the previous two tutorials can be represented as a template.

 

Class Name

Message

 

Item 1

TO

Item 2

FROM

Item 3

SUBJECT

Item 4

BODY

 

The template is the model for defining the form of data and the term class can be applied to the template.   Once the class has been defined, instances of the class can be created and memory can be allocated for all components of the definition. The instances can then, be manipulated and an instance can be defined as an object.

 

Hierarchical trees, nodes and node lists

 

It was noted in the previous tutorial that there is a relationship between the tag and the data in the xml file.  So it is possible to create hierarchical trees to represent the structure of the HTML file.  This natural correspondence is used to model the organization of web sites which use xml. 

 

The terminal branches of the tree are known as nodes and the tag pairs are used to read data from the xml file by the xmldom object to create an instance of a node class.  The node object contains a nodeName property.  The node is the basic component of the way the data is represented in memory.

 

Tag pairs which are used to create nodes at the same level are aggregated into a nodeList object.  The childNodes property of the root element contains an instance of the nodeList object.  The length property of the node list stores the list of childNodes that are contained in the array and this can be manipulated to create flexible code. 

 

To illustrate this, let us create a small program:

 

The program will determine the number of nodes in the list of data nodes and then loop through the array to extract both the name and the contents of the node.  The values will then be inserted into a HTML file. 

 

<html>

<head>

<Title> Creating nodeLists in xml and reading it</title>

<script language = “JavaScript”>

<!--

var myrootelement

var mynodecount

var xmlDoc1=new ActiveXObject(“microsoft.xmldom”);

xmlDoc1.load(“myxml.xml”);

 

Function start()

{

If(xmlDoc1.readyState=“4”)

{

StartLoading();

}

Else

{

Alert(“Failed to load”);

}

}

 

Function StartLoading()

{

var index

myrootelement.xmlDoc1.documentElement;

mynodecount=myrootelement.childNodes.length;

for(index=0;index<nodecount;index++)

document.write(myrootelement.childNode.item(index).nodeName+“:”);

document.write(myrootelement.childNodes.item(index).text+“<BR>”);

}

}

</script>

</head>

<body onload=“start()”>

</body>

</html>

 

 

 

Now let us create the xml file

 

<?xml version=“1.0”?>

 

<Message>

<To> Recipient</To>

<From>Sender</From>

<Subject>Test</Subject>

<Body> This is a test case</Body>

</Message>

 

Use the html file to read this xml file.

 

Save both files in the same directory else specify the path of the xml file in the HTML.

 

Error Handling

 

Everyone commits mistakes.  It is ok to commit mistakes.  It is not Ok to be in ignorance of the mistake committed.  Error handling is all about becoming aware of errors in the code at run time.  This becomes all the more urgent and imperative when you are using the code to display data on the web where you cannot see or interact with your clients face to face.

 

Let us say that the xml document that you want to open has been deleted inadvertently.  How will you know that?  The least you can do is ensure that you get a message if the xml file is missing. This error can be caught by simply adding an alert to the code at the appropriate place.  Go back to the code in the HTML file and add the following line to the code after you have created the ActiveXObject.

 

var xmlDoc1=new ActiveXObject(“Microsoft.xmldom”);

alert(xmlDoc1);

 

This will help coders to gracefully exit their code when the code is invalid.

 

 

In this tutorial we have learnt that while xml is not object oriented, there are areas of common interest between OO and xml.  OO like implementations can be used to an advantage.

 

In the next tutorial we shall study how to insert xml code directly into HTML and dynamically update the contents of a node.