XML Tutorials

  Home arrow XML Tutorials arrow Page 5 - Handling XML Data for Ajax
XML TUTORIALS

Handling XML Data for Ajax
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2008-01-03

    Table of Contents:
  • Handling XML Data for Ajax
  • Back on the Client: Mining the XML
  • XML Parsing with JavaScript
  • Populating the Form on a Microsoft Browser
  • Populating the Form on Other Browsers

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Handling XML Data for Ajax - Populating the Form on Other Browsers


    (Page 5 of 5 )

    The code for handling non-Microsoft browsers, shown in Example 4-7, is similar; there are some minor differences in the parser API, but that's about it.

    Example 4-7. The nonMSPopulate( ) function

     1 function nonMSPopulate() {
     2     var resp = req.responseText;
     3     var parser = new DOMParser();
     4     var dom = parser.parseFromString(resp,"text/xml");
     5
     6     decVal = dom.getElementsByTagName("decimal");
     7     var decimal = document.getElementById('decimal');
     8     decimal.value=decVal[0].childNodes[0].nodeValue;
     9
    10     hexVal = dom.getElementsByTagName("hexadecimal");
    11     var hexadecimal = document.getElementById('hexadecimal');
    12     hexadecimal.value=hexVal[0].childNodes[0].nodeValue;
    13
    14     octVal = dom.getElementsByTagName("octal");
    15     var octal = document.getElementById('octal');
    16     octal.value=octVal[0].childNodes[0].nodeValue;
    17
    18     hyperVal = dom.getElementsByTagName("hyper");
    19     var hyper = document.getElementById('hyper');
    20     hyper.value=hyperVal[0].childNodes[0].nodeValue;
    21
    22     binaryVal = dom.getElementsByTagName("binary");
    23     var bin = document.getElementById('bin');
    24     bin.value=binaryVal[0].childNodes[0].nodeValue;
    25 }

    We create a newDOMParser(line 3), then we create a DOM on line 4 from the XML string that we received from the servlet. Next, we get the element between the<decimal></decimal>tags (line 6) by callingdom.getElementByTagName("decimal"). After retrieving thedecimalform element from our HTML document, we retrieve the value sent to us in the XML and use it to set the appropriate field:

      decimal.value=decVal[0].childNodes[0].nodeValue;

    The reference todecVal[0]gets the data between the<decimal></decimal>tags. If we had two sets of<decimal></decimal>tags, we would reference the second set asdecVal[1].

    This is what the data should look like in the response sent from the servlet:

      <converted-values>
          <decimal>97</decimal>
          <hexadecimal>0x61</hexadecimal>
          <octal>0141</octal>
          <hyper>&amp;0x61;</hyper>
          <binary>1100001B</binary>
      </converted-values>

    Please check back next week for the conclusion to this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    blog comments powered by Disqus

    XML TUTORIALS ARTICLES

    - Validation with Document Type Definitions (D...
    - Creating a Well-Formed XML Document
    - Getting to Know XML
    - A Friendly Approach to XML
    - Creating RSS 2.0 Feeds
    - Using Modules in Your RSS Feed
    - RSS 2.0
    - Querying XML: Use Cases
    - Joins and Query Use with XML
    - Solving Problems by Querying XML
    - Performing Set Operations When Querying XML
    - Querying XML
    - Handling Data for Ajax with JSON
    - Handling XML Data for Ajax
    - XML and JSON for Ajax


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap