Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Ajax
|
Ajax the combination of XHTML, CSS, DOM, XML, XSLT, XMLHttpRequest, and JavaScript
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Ajax section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 18th, 2008, 10:24 PM
Registered User
 
Join Date: Sep 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ajax to handle xmlresponse

Hi All

I am new to Ajax.
I am trying to implement Google Suggest for my application.
I create one test application to start with ajax.
My servlet response me one String = "<Categories><Category><name>NOTICE OF VALUATION OF REAL ASSETS</name></Category><Category><name>NOTICE OF INTEREST/CHANGES IN INTEREST</name></Category><Category><name>NOTICE OF BOOK CLOSURE DATE</name></Category><Category><name>Notice of 3 Consecutive Years Losses</name></Category></Categories>"
<-------------------Below is my Javascript code to handle it------->

function par****MLValue()
{
    alert("inside par****MLValue");
    var xmlValue = new ActiveXObject('MSXML.DomDocument');
    alert("inside par****MLValue" + 1);
    xmlValue.async = false;
    alert("inside par****MLValue" + 2 + mkArray);
    xmlValue.loadXML("<Categories><Category><name>NOTI CE OF VALUATION OF REAL ASSETS</name></Category><Category><name>NOTICE OF INTEREST/CHANGES IN INTEREST</name></Category><Category><name>NOTICE OF BOOK CLOSURE DATE</name></Category><Category><name>Notice of 3 Consecutive Years Losses</name></Category></Categories>");
    alert("inside par****MLValue" + 3);
    var root = xmlValue.getElementsByTagName('Categories')[0];
    alert("inside par****MLValue" + root.xml);
    var items = root.getElementsByTagName("Category");
    alert("value of items" + items.length);
    for (var i = 0 ; i < items.length ; i++) {
        // get one item after another
        alert("loop no:" + i);
        var item = items[i];
        // now we have the item object, time to get the contents
        // get the name of the item
        var name = item.getElementsByTagName("name")[0].firstChild.nodeValue;
        alert("loop no:" + i + "\t" + "name:" + name);
        xmlarr[i] = name;// My loop doesn't go beyond to this point
        alert("name=" + name + ": xmlarr" + xmlarr[i]);
    }
    alert("outside for loop");

}

Please let me know where exactly i am wrong.
Thanks IN advance

 
Old September 18th, 2008, 11:27 PM
jminatel's Avatar
Wrox Staff
Points: 18,059, Level: 58
Points: 18,059, Level: 58 Points: 18,059, Level: 58 Points: 18,059, Level: 58
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2003
Posts: 1,906
Thanks: 62
Thanked 139 Times in 101 Posts
Default

Sorry about the forum butchering your code. I assume you meant par****MLValue to be parseXMLValue. I think I'd adjusted the filter to allow this to work in future posts.

Jim Minatel
Acquisitions Director
Wiley Technology Publishing
WROX Press
Blog: http://wroxblog.typepad.com/
Wrox online library: http://wrox.books24x7.com
 
Old September 19th, 2008, 04:02 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I can't see why it's not working but it's difficult to read. Here's a working example:
Code:
<html>
<head>
  <title>Show Categories</title>

  <script type="text/javascript">
    function getXmlFromServer()
    {
      return "<Categories>"
      + "<Category><name>NOTICE OF VALUATION OF REAL ASSETS</name></Category>"
      + "<Category><name>NOTICE OF INTEREST/CHANGES IN INTEREST</name></Category>"
      + "<Category><name>NOTICE OF BOOK CLOSURE DATE</name></Category>"
      + "<Category><name>Notice of 3 Consecutive Years Losses</name></Category>"
      + "</Categories>";
    }

    function getParser()
    {
      //Here I would use something like zXML
      //to allow cross-browser usage
      var oDom = new ActiveXObject("msxml2.domdocument.3.0");
      oDom.setProperty("SelectionLanguage", "XPath");
      oDom.async = false;
      return oDom;
    }

    function showCategories()
    {
      var oDom = getParser();
      var sXml = getXmlFromServer();
      var bLoaded = oDom.loadXML(sXml);
      if (!bLoaded)
      {
        alert(oDom.parseError.reason);
        return;
      }
      //alert(oDom.xml);
      var arrData = [];
      //Using DOM
      var colCategories = oDom.documentElement.getElementsByTagName("Category");
      //alert(colCategories.length);
      for (var i = 0, l = colCategories.length; i < l; i++)
      {
        var oCategory = colCategories[i];
        var oName = oCategory.getElementsByTagName("name")[0];
        arrData[i] = oName.firstChild.nodeValue;        
      }
      alert(arrData.join("|"));
      //Using XPath
      var colNames = oDom.selectNodes("/Categories/Category/name");
      for (var i = 0, l = colNames.length; i < l; i++)
      {
        arrData[i] = colNames[i].firstChild.nodeValue;
      }
      alert(arrData.join("|"));
    }

  </script>

</head>
<body onload="showCategories();">
</body>
</html>

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Recommend an AJAX IDE - JoyiStar AJAX WebShop. kingstar Ajax 4 December 15th, 2006 05:12 AM
New Ajax article: Creating an Ajax Search W jminatel BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 May 11th, 2006 03:45 PM
new Ajax article: Creating an Ajax Search Widget jminatel Ajax 0 May 11th, 2006 02:50 PM
New Ajax Article: Ajax Submission Throttling jminatel Ajax 0 April 11th, 2006 08:00 PM
How to handle...? chawnu Pro PHP 1 December 8th, 2005 05:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.