spacer
home / programming / javascript / jf / column13 / 1 To page 1current pageTo page 3
[previous][next]

ASP 3.0/.NET Developer
Jupitermedia
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear

How to Develop Web Applications with Ajax: Pt. 2

Before We Continue...

I'd like to point out a couple of options:

First, you’ll notice we didn’t use the <pets> node. This is because we have only one data group (<pets>) and all the subsequent elements (each <pet> element); contains different data but they have the same name. In our case, this node could be omitted. However, it’s better to have <pet> elements inside a <pets> element, rather than to have a bunch of <pet> elements on their own (but still inside the <data> element).

Another way this could be done is by having a specific tag for each pet. For example:

     <?xml version="1.0" encoding="UTF-8"?>
    <data>
      <pets>
        <Cat />
        <Dog />
        <Fish />
      </pets>
    </data>

Then we could loop through the nodes within the <pets> element. The processXML function would then look like this:

        function processXML(obj){
          var dataArray = obj.getElementsByTagName('pets')[0].childNodes;
          var dataArrayLen = dataArray.length;
          var insertData = '<table style="width:150px; border: solid 1px #000"><tr><th>'
         + 'Pets</th></tr>';
        for (var i=0; i<dataArrayLen; i++){
           if(dataArray[i].tagName){
              insertData += '<tr><td>' + dataArray[i].tagName + '</td></tr>';
           }
        }
        insertData += '</table>';
        document.getElementById ('dataArea').innerHTML = insertData;
      }

What’s changed here is that we refer to the <pets> group element (the “[0]” means the first one, even though it’s the only one) and then its child nodes (the <Cat />, <Dog /> and <Fish /> elements). Since text nodes separate these nodes (the whitespace is considered a node), we need to make sure that only the nodes with tag names (that is, only the tags) get through. Then we output the name of each tag. Since the name of each tag is a pet, we don’t need to have data for each node – the name of the node itself will suffice. Give this a shot to see how it works.

Yet another way to do this is set an attribute value for each <pet> node. Your XML document would look like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <data>
      <pets>
        <pet type="Cat" />
        <pet type="Dog" />
        <pet type="Fish" />
      </pets>
    </data>

You would change your processXML function just slightly, so that it becomes:

        function processXML(obj){
          var dataArray = obj.getElementsByTagName('pet');
          var dataArrayLen = dataArray.length;
          var insertData = '<table style="width:150px; border: solid 1px #000"><tr><th>'
          + 'Pets</th></tr>';
        for (var i=0; i<dataArrayLen; i++){
              insertData += '<tr><td>' + dataArray[i].getAttribute('type') + '</td></tr>';
          }
          insertData += '</table>';
          document.getElementById ('dataArea').innerHTML = insertData;
       }

The key difference here is we get the value from dataArray[i].getAttribute('type'). This takes the value of the current <pet> node's "type" attribute.

Moving On...

Now that we've covered some useful ways to retrieve data from a single group of XML data, let's look at how to retrieve data from multiple groups. Instead of just listing the pets one may own, let's suppose that we have a daily schedule for our pets. Each pet must be taken care of properly, since they have special needs. The caretaker of the animals meets these needs on a daily basis. Here's how to place this into well-formed XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <data>
      <pets>
        <pet>Cat
          <task>Feed</task>
          <task>Water</task>
          <task>Comb out fleas</task>
        </pet>
        <pet>Dog
          <task>Feed</task>
          <task>Water</task>
          <task>Put outside</task>
        </pet>
        <pet>Fish
          <task>Feed</task>
          <task>Check oxygen, water purity, etc.</task>
        </pet>
      </pets>
    </data>

This might seem strange, but what we're creating sub-groups. Each <pet> element is a sub-group of the <pets> group, while each <task> is a sub-element of each <pet> group.

Before I go on, you may wish to format your tables using some CSS, like this:

      <style type="text/css"><!--
      table, tr, th, td {
        border: solid 1px #000;
        border-collapse: collapse;
        padding: 5px;
     }
    --></style>

This makes that table easier to read. Now let's dive into that processXML function:

      function processXML(obj){
        var dataArray = obj.getElementsByTagName('pet');
        var dataArrayLen = dataArray.length;
        var subAry, subAryLen;
        var insertData = '<table><tr><th>'
          + 'Pets</th><th>Tasks</th></tr>';
        for (var i=0; i<dataArrayLen; i++){
              insertData += '<tr><td>' + dataArray[i].firstChild.data + '</td>';
              subAry = dataArray[i].getElementsByTagName('task');
              subAryLen = subAry.length;
              insertData += '<td>';
                for(var j=0; j<subAryLen; j++){
                  insertData += subAry[j].firstChild.data;
                  if( subAryLen != j+1 ) { insertData += ', '; }
                }
            insertData += '</td></tr>';
      }
      insertData += '</table>';
      document.getElementById ('dataArea').innerHTML = insertData;
    }

The first new thing is the declaration of two variables: "subAry" and "subAryLen." These are similar to the "dataArray" and "dataArrayLen" variables, except they reference different arrays (specifically, they will reference the "task" elements, while the "dataArray" and "dataArrayLen" reference the "pet" elements).

We also changed the initial value of the "insertData" variable - we added a new table header (<th>) for our "tasks" column.

home / programming / javascript / jf / column13 / 1 To page 1current pageTo page 3
[previous][next]


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
How to Create an Ajax Autocomplete Text Field: Part 6 · Software Engineering for Ajax · Perl Pragma Primer
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Using File Virtualization for Disaster Recovery · VoIP Security: SIP—Versatile but Vulnerable · Around the World in 80 Nodes

Created: March 27, 2003
Revised: October 7, 2005

URL: http://webreference.com/programming/javascript/jf/column13/1