到 Google 资讯主页   
EasyJF首页   资料   源码   软件    论坛   网站    
   使用帮助    
    该信息为本站MyRSS系统缓存内容,部分图片及附件有可能无法正常使用.easyjf.comDevX Java信息无关,不对该信息负责.通过http://kb.csdn.net/keyword/java//../../java/Articles/200608/0a090365-8e66-400c-bfa5-d85c6a7a77b5.html访问该信息的原始内容.
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   
Traverse XML Data Using JavaScript
作者: 来源:DevX Java信息  发布时间:2006-08-16 00:00:00.0

n this article, I'll show you how to build an XML-based, client-side JavaScript app that reads data from external XML files, traverses the XML data, and displays that data in a tree format. The sample code uses the XMLDOM ActiveX object built into Microsoft Internet Explorer.

Say you have the following personal data and you want it displayed in a structured manner:

  • name: Premshree Pillai
  • sex: male
  • Websites: Websites
  • ws1: http://www.qiksearch.com
  • ws2: http://premshree.resource-locator.com
The XML looks like this:

<?xml version="1.0"?>
<personal>Personal Details
        <name>Premshree Pillai</name>
        <sex>male</sex>
        <websites>Websites
                <ws1>http://www.qiksearch.com</ws1>
                <ws2>http://premshree.resource-locator.com</ws2>
        </websites>
</personal>
This is the algorithm you'd use:
  1. Read the XML file
  2. Point a variable, tree to the first node (XML tag) of the XML data.
  3. If the node has child nodes:
    • Print "<ul><li>";
    • For each child node, traverse(tree.childNodes(nodeNum))
    • Print "</li></ul>";
  4. If the node does not have any child:
    • Print the node's value.



The Script
This code creates a new instance of the Microsoft.XMLDOM ActiveX object:

var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
Then, the loadXML() function loads a particular .xml file:

function loadXML(xmlFile) {
        xmlDoc.async="false";
        xmlDoc.onreadystatechange=verify;
        xmlDoc.load(xmlFile);
}
The loadXML() function then references the verify() function:

function verify() { 
        if(xmlDoc.readyState!=4)
                return false; 
}
The process of loading of a XML file has five stages:
  • 0 - Object is not initialized
  • 1 - Loading object is loading data
  • 2 - Loaded object has loaded data
  • 3 - Data from object can be worked with
  • 4 - Object completely initialized
Using the XMLDOM's readyState property to determine your XML file's state of loading can be more trouble than it's worth. If a file (object) is not initialized, xmlDoc.readyState returns 0—and so on. Therefore, to avoid partially or uninitialized objects, use the loadXML() function.

Traverse() is the function that performs XML data traversal. It is a recursive function that takes a node as it's argument:

 
function traverse(tree) {
        if(tree.hasChildNodes()) {
                document.write('<ul><li>');
                document.write('<b>'+tree.tagName+' : </b>');
                var nodes=tree.childNodes.length;
                for(var i=0; i<tree.childNodes.length; i++)
                        traverse(tree.childNodes(i));
                document.write('</li></ul>');
        }
        else
                document.write(tree.text);
}



What the Script Does
First the function checks if the node has any children. If it does, the necessary indentation is done using HTML lists (<ul>,<li> tags). Next, for each child node of this node, the function traverse() is called (recursively) with the argument as that child node.

If the node argument passed to traverse() is childless, the function prints the value held by that node (tag). In this way, the tree structure for the XML file is generated.

To generate the tree structure of a XML file, use the initTraverse() function. This function takes the XML filename as it's argument. It loads the XML file, then sets the variable doc to the root node of the XML data, and finally traverses the XML data using the traverse() function with an argument as the root node, i.e doc.

 
function initTraverse(file) {
        loadXML(file);
        var doc=xmlDoc.documentElement;
        traverse(doc);
}

Place the following code where the tree form of a XML file has to be generated:

 
initTraverse("anyXMLfile.xml");
All of this code can be placed in an external .js file.

Script Listing
The following is the complete listing of the client-side JavaScript app.


<script language="JavaScript">
//////////////////////////////////////////////////
//      XML Data Traversal                      //
//      (c) 2003 Premshree Pillai               //
//      http://www.qiksearch.com                //
//      http://premshree.resource-locator.com   //
//      Email : qiksearch@rediffmail.com        //
//////////////////////////////////////////////////

var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
function loadXML(xmlFile) {
        xmlDoc.async="false";
        xmlDoc.onreadystatechange=verify;
        xmlDoc.load(xmlFile);
}

function verify() { 
        if(xmlDoc.readyState!=4)
                return false; 
}

function traverse(tree) {
        if(tree.hasChildNodes()) {
                document.write('<ul><li>');
                document.write('<b>'+tree.tagName+' : </b>');
                var nodes=tree.childNodes.length;
                for(var i=0; i<tree.childNodes.length; i++)
                        traverse(tree.childNodes(i));
                document.write('</li></ul>');
        }
        else
                document.write(tree.text);
}

function initTraverse(file) {
        loadXML(file);
        var doc=xmlDoc.documentElement;
        traverse(doc);
}
</script>
Moving Forward
The above script can be extended to serve a variety of purposes. For instance, you could use this JavaScript to display a sitemap by simply defining its structure in an XML file.

Premshree Pillai studies engineering in Information Technology at Mumbai University, Mumbai, India. He is a programming enthusiast and maintains a Website, where he posts his scripts. He is also a freelance writer and has written for a range of popular Indian magazines.


DevX is a division of Jupitermedia Corporation
© Copyright 2005 Jupitermedia Corporation. All Rights Reserved. Legal Notices

 
相关文章
 
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   


EasyJF.com 2006 隐私政策 使用EasyJF前必读