spacer
home / programming / javascript / jf / column13 / 1 To page 1To page 2current page
[previous]

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

The next change comes inside of the for loop: we set values to the subAry and subAryLen variables. The subAry variable becomes an array of the <task> elements of the current <pet>. The subAryLen variable becomes the length of that array until it changes again (when the outer for loop starts over with the next <pet>).

We create an inner for loop to process all the <task> elements, one at a time. Basically, we create a new data cell, input a comma-delimited list of tasks in it, then close the data cell and the current row. Specifically, the current <task> element’s node data (the task itself; for example, “Feed”) is placed into a data cell inside the “insertData” variable.

After that, we check to see if there are any more tasks for the current <pet>. If there are more, we append a comma (,) to the insertData variable so that each task is delimited with a comma (as “a, b, c” instead of “a, b, c,” – note the last comma on the second task, which we do not want). This is done by checking the length of the subAry array against the for loop's “j” variable plus one. Since the loop increments “j,” in the next loop, the “j” will equal one more than it does at the time of the check. Therefore, if “j+1” (or, “what j will equal when and if the loop starts again”) equals subAryLen (the number of <task> nodes within the current <pet> node), the loop won’t run again. If the loop doesn’t run again, we don’t want to add another comma to separate the tasks. So if “j+1” is NOT equal to subAryLen, we know that we can safely add a comma to “insertData” in preparation for the next <task>.

Once the inner for loop has ended, we close the task data cell and that pet’s row. The outer loop starts over by creating a new row and moving to the next <pet>. This process repeats until all <pet> elements (and all <task> elements for each pet) have been processed.

Is There Another Way?

You might be thinking: “That JavaScript can get pretty complex, but it only gets more complex each time the XML gets more complex. Maybe we can simplify the XML to, in turn, simplify the JavaScript.” If you are, that’s great, because you’re absolutely correct. Of the different methods I showed earlier, the one I chose to elaborate on might appear to be the least desirable. What if we chose to use attributes for both the pet and the corresponding tasks? What would the XML look like?

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

Wow! This is much simpler. Let’s look at how our processXML function will change:

      function processXML(obj){
        var dataArray = obj.getElementsByTagName('pet');
        var dataArrayLen = dataArray.length;
        var insertData = '<table><tr><th>'
         + 'Pets</th><th>Tasks</th></tr>';
        for (var i=0; i<dataArrayLen; i++){
            insertData += '<tr><td>' + dataArray[i].getAttribute('type')             + '</td>'
    + '<td>' + dataArray[i].getAttribute('tasks') + '</td></tr>';
        }
        insertData += '</table>';
        document.getElementById ('dataArea').innerHTML = insertData;
    }

As you may have suspected, the function is much simpler. When code is simpler, it’s usually more efficient. The only difference between this and one of our older functions is that the insertData variable now gets more HTML inserted into it, specifically two new attributes: “type” and “tasks.” As we learned earlier, these are the attributes we gave to the <pet> elements in our XML document, and each pet’s attributes has different values. This is probably the easiest way to modify the XML yourself to fit fluctuations in your schedule. e.g., If you finally get rid of your cat’s fleas, you can easily remove “Comb for fleas” from the list of daily tasks for your cat, whereas with the XML we used earlier, that would have been confusing to implement.

The last XML formatting method is a mixture of two. Here, we’ll use attributes and different tags. Let’s have a look at some example XML:

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

This is probably the most comprehensible XML yet. Let's analyze the changes we'll need to make to our processXML function in order for everything to work:

      function processXML(obj){
        var dataArray = obj.getElementsByTagName('pets')[0].childNodes;
        var dataArrayLen = dataArray.length;
        var insertData = '<table><tr><th>'
         + 'Pets</th><th>Tasks</th></tr>';
        for (var i=0; i<dataArrayLen; i++){
          if(dataArray[i].tagName){
            insertData += '<tr><td>' + dataArray[i].tagName + '</td>'
                + '<td>' + dataArray[i].getAttribute('tasks') + '</td></tr>';
          }
      }
      insertData += '</table>';
      document.getElementById ('dataArea').innerHTML = insertData;
    }

The “dataArray” now references the <pets> child nodes as an array. (In other words, dataArray is now an array of all the nodes within the <pets> node.) This is because each tag is different (<Cat />, <Dog />, and <Fish />), so we can’t search for these elements by their name (as we could with <pet>, since they were all <pet> elements).

Once again, there is whitespace between these nodes, so text nodes need to be eliminated from our processing. We can do this by checking to see if a tag name is present – text nodes are nodes but not tags, while the <Cat />, <Dog /> and <Fish /> nodes are all tags. If a tag name is present, we can insert new data into the insertData variable. The data we insert is a table row and two table data cells. The first cell is the tag name, which is the type of pet (Cat, Dog or Fish), and the second cell is the value of the “tasks” attribute for that specific animal (such as “Feed or Water”).

Closing Notes

There are many more variations of the examples I’ve demonstrated in this article, so feel free to test them to see which is right for you. Just remember that XML is “eXtensible” so there’s no “wrong” way to group your data, though there’s often a “best” way. Also, keep in mind that your XML should be well-formed. Bear in mind that most problems come from forgetting to close a tag (as in <Dog /> instead of <Dog>; unless you have data that goes inside that node, in which case you would put <Dog>data here</Dog>).

It’s my intention that this use of XML and JavaScript isn’t confusing, but enlightening. By gradually learning to deal with more data, you can put Ajax to greater use. I’d love to see more of Ajax on corporate sites, etc., so if you’ve put this knowledge into practice, I'd like to know about your experiences.

About the Author

Jonathan Fenocchi is a Web developer who primarily works in the fields of Web Design, client-side scripting, and PHP scripting. His web site is located at: www.slightlyremarkable.com


home / programming / javascript / jf / column13 / 1 To page 1To page 2current page
[previous]



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