spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / xml current pageTo page 2To page 3
[next]

IE XML Data Island Functionality in NS6+ Browsers

Finance Developer (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume
Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans


In this article we'll take a closer look at 3 different approaches that you can incorporate to import/embed XML in your HTML pages. This article is divided into 3 sections:

About XML and Its Role

In essence the XML acronym says it all: eXtensible Markup Language. The heart of XML is its ability to help separate content from presentation for the web. It's called extensible because unlike HTML (where a developer has a limited number of tags whose behavior is predefined), XML can create new elements with new behaviors. XML is written in SGML which is the international standard metalanguage for text markup systems (ISO 8879).

IE XML Data Island Functionality in NS6+ browsers.

What is the <xml> Element?
<xml> is an html tag that defines an XML data island on an HTML page, and was introduced by Microsoft with the release of the IE5.0 browser. An XML data island allows you to embed/import an XML document straight to your HTML page, where XML data can be manipulated by using DOM methods with JavaScript.

To use this element in your HTML pages, there are multiple options available:

Below are some attributes that are used in this tag:

ID Its used to get a reference to this object (XML Data Island)
SRC Only used when an XML file is imported

Microsoft has a detailed API listed here [(MSDN Library)] for this tag. Let's see how the SRC attribute works when we import an XML document. The first thing to do is to import an XML file (below is a sample XML file):

  <xml version="1.0" encoding="UTF-8"?>
    <employees>
     <employee>
    		<name>Name</name>
    		<job>Job</job>
    		<department>Department</department>
    		<cubicle>Cubicle</cubicle>
    	</employee>
    
    	<employee>
    		<name>Joe</name>
    		<job>Programmer</job>
    		<department>Engineering</department>
    		<cubicle>5E</cubicle>
    	</employee>
      
        <employee>
    		<name>Jane</name>
    		<job>Desigmer</job>
    		<department>Architecture</department>
    		<cubicle>12A</cubicle>
    	</employee>
    </employees>
    

The file structure itself is self-explanatory. We have the root element <employees>, then we have the <employee> block with information about each employee. To begin the first part of import process, we need to define an XML data island element:

      <xml id="xmlIsland" src="resources/employee_db.xml">></xml>
    
Where ID ="xmlIsland" will be the Data Island reference for further use. Now we need to create an XMLDocument object.
      var xmlDoc = document. all("testXML").XMLDocument;
    

At this point xmlDoc contains the entire XML document tree structure, where we can access any node/value using JavaScript by utilizing standard DOM methods. DOM is a powerful tool to manipulate XML document. Below is the logical flow to parse this XML document for the purpose of this example:

1. Get the root element (in this case, <employees>).
2. Loop through all of the child nodes for this element.
3. Retrieve the node name and node value, if present, and then display.

The W3C DOM is a handy and powerful tool to perform such tasks. Now, we have another variable declared xmlStr="", which will hold the results of each element (node name and it's value) for this XML document. The next step in this logical flow is a call to a function that will initiate the entire walking through the DOM tree process.

      parseXML(parentNode);
    

Note: If you're planning to work with XML, remember recursion, recursion, recursion.... Make sure that you're comfortable using recursive logic, because that’s how you write efficient code to parse XML data. See the code below:

      function parseXML(node){
      	if(node.nodeName=="employee"){xmlStr+="<br/><br/>";}
        if(node.hasChildNodes && node.childNodes[0].nodeType!=3){
          for(var n=0;n&let;node.childNodes.length;n++){
            parseXML(node.childNodes[n]);
          }
        }else{
          xmlStr+="name = "+node.nodeName+", value = "+node.childNodes[0].nodeValue+"<br/>";
        }
      }
    

In the first line of the function, you add a couple of break tags for the formatting purposes, nothing more. The actual processing begins in the second line.
      if(node.hasChildNodes && node.childNodes[0].nodeType!=3){
    

This line ensures that we recursively call this function only if the current node has child nodes attached to it and that the first child is not that of nodeType text. If this condition isn't met, we append the node name and the node value to the xmlStr which is printed to display the node name and node value. View the Complete Working Example.

In the following pages, you'll that there is not much difference in parsing XML in all three approaches. The only difference is the way we import or embed the XML in the document. Once that's been taken care of, rest is using the DOM methods with JavaScript to walk through the XML document tree.

home / programming / javascript / xml current pageTo page 2To page 3
[next]


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger

Created: June 2, 2003
Revised: June 2, 2003

URL: http://webreference.com/programming/javascript/xml