XML Tutorials
  Home arrow XML Tutorials arrow Page 3 - XML and JSON for Ajax
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
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 / 3
    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

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    XML and JSON for Ajax - Other Ways to Build the XML Document


    (Page 3 of 4 )

    Building an XML document by appending to a StringBuffer is a common approach, but it's far from ideal, particularly if you need to generate a large document programmatically. Fortunately, there are alternatives.

    JDOM

    One option is to use the JDOM library to write the XML. Download the jdom.jar file from http://www.jdom.org, and put it in your application's WEB-INF/lib directory. Then, instead of writing to a StringBuffer, use JDOM to build the XML, as shown in Example 4-2.

    Example 4-2. Using JDOM to create the XML document

    // additional imports needed for JDOM
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.output.XMLOutputter;

    public String createJdomXML(int key) throws IOException {
        Document document = new Document();
        // create root node
        Element root = new org.jdom.Element("converted-values");
        document.setRootElement(root);

        // create your node
        org.jdom.Element element = new org.jdom.Element("decimal");
        // add content to the node
        element.addContent(Integer.toString(key));
        // add your node to root
        root.addContent(element);

        element = new org.jdom.Element("hexadecimal");
        element.addContent("0x" + Integer.toString(key, 16));
        root.addContent(element);
        element = new org.jdom.Element("octal");
        element.addContent("0" + Integer.toString(key, 8));
        root.addContent(element);
        element = new org.jdom.Element("hyper");
        element.addContent("&0x" + Integer.toString(key, 16));
        root.addContent(element);
        element = new org.jdom.Element("binary");
        element.addContent(Integer.toString(key, 2) + "B");
        root.addContent(element);

        // output JDOM document as a String of bytes
        XMLOutputter outputter = new XMLOutputter();
        return outputter.outputString(document);
    }

    In the preceding code, we first create aDocument (org.jdom.Document), then anElementnamedrootwith theString "converted-values"as its value. That element becomes the root of the XML document. Here's what the document looks like at this point:

      <converted-values>
      </converted-values>

    To add child elements to the root, we create new elements and add them to the root element. The code that creates thedecimal element and adds it to the root element looks like this:

      org.jdom.Element element = new org.jdom.Element("decimal");
      element.addContent(Integer.toString(key));
      root.addContent(element);

    We repeat this process until we've added all the elements to the root. Then we use anXMLOutputter to format the document into aString that we can send back to the client. The JDOM XML document now looks like this (with linefeeds and spaces added for readability):

      <?xml version="1.0" encoding="UTF-8"?>
      <converted-values>
          <decimal>97</decimal>
          <hexadecimal>0x61</hexadecimal>
          <octal>0141</octal>
          <hyper>&amp;0x61</hyper>
          <binary>1100001B</binary>
      </converted-values>

    More XML Tutorials Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Ajax on Java," published by O'Reilly. We...
     

    Buy this book now. This article is excerpted from chapter four of the book Ajax on Java, written by Steven Douglas Olson (O'Reilly, 2007; ISBN: 0596101872). Check it out today at your favorite bookstore. Buy this book now.

    XML TUTORIALS ARTICLES

    - 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-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT