XML Tutorials

  Home arrow XML Tutorials arrow XML and JSON for Ajax
XML TUTORIALS

XML and JSON for Ajax
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2007-12-27

    Table of Contents:
  • XML and JSON for Ajax
  • Setting Up a Simple XML Document
  • Other Ways to Build the XML Document
  • dom4j

  •  
     

    SEARCH CODEWALKERS

    XML and JSON for Ajax


    (Page 1 of 4 )

    XML is helpful for Ajax applications when you're dealing with multiple data points, as is common with web applications. This article, the first of three parts, helps get you started using XML with Ajax. It is excerpted from chapter four of the book Ajax on Java, written by Steven Douglas Olson (O'Reilly, 2007; ISBN: 0596101872). Copyright 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    Do you really need XML for an Ajax application? The previous chapter showed that you don't always need XML. In particular, if you only have one data point, XML is overkill. But the fact is, most web applications deal with multiple data points: usernames, passwords, addresses, cities, states, zip codes, etc. How will you decipher those fields when they're sent back from the server?

    In some cases, passing a string of delimited values may seem like the simplest approach, but using XML has advantages. For one thing, XML is self-documenting. During debugging, you can look at the XML string and see exactly what goes where; that is a luxury you won't have with a string of comma-separated values.

    Another reason for using XML is that an XML parser is built into most browsers. The parsing work has already been done for you; all you have to do is leverage the built-in parser. Sure, you could pass the data in other formats--Java properties files, comma or tab-separated values, YAML files, or a cute custom format that you've designed yourself--but then you would have to write your own parser in JavaScript.

    There is another good way to send data to and from the server: JavaScript Object Notation (JSON). We will discuss JSON toward the end of this chapter.

    The Character Decoder

    The example in this chapter is similar to the one in the previous chapter, but instead of the server returning one data point, it's going to return five. Retuning a small collection of data shows what happens when you go beyond a single data point and illustrates why most Ajax applications need XML or some other way to structure the data that is passed from the server to the client.

    Figure 4-1 shows how the user interface of the application will look when we're done. The design is simple enough: we send a character to the server usingXMLHttpRequest(), and the server responds with aString containing the five conversions in XML format (decimal, octal, hexadecimal, binary, and HTML). Thecallback()function in the client then calls a function to parse the XML and populate the fields in the browser.


    Figure 4-1.  The complete Ajax Character Decoder example

    Now it starts to get fun. One keystroke fills in all the data fields, and although it doesn't look like much is going on from the user's perspective, from the programmer's perspective we know that the application is communicating with the server without a clunky submit or reload or any waiting for the page to refresh.

    More XML Tutorials Articles
    More By O'Reilly Media

    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

    Developer Shed Affiliates

     



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