XML Tutorials

  Home arrow XML Tutorials arrow Page 3 - 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

    TOOLS YOU CAN USE

    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

    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 3 - Follow our Sitemap