XML Tutorials

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


    (Page 4 of 4 )

     

    dom4j is an XML library similar in intent to JDOM. After downloading dom4j from http://www.dom4j.org/download.html and installing it in your application's WEB-INF/lib directory, you can use it to create your XML document. As shown in Example 4-3, we create a document, add a root element to the document, add the elements and data to the root, and then return the document in a String.

    Example 4-3. Using dom4j to create the XML document

    // additional imports for dom4j
    import org.dom4j.Document;
    import org.dom4j.DocumentHelper;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.XMLWriter;

    ...

    public String createDom4jXML(int key) throws IOException {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("converted-values");

        Element element = root.addElement("decimal").addText(
                Integer.toString(key));
        element = root.addElement("hexadecimal").addText(
               
    "0x" + Integer.toString(key, 16));
        element = root.addElement("octal").addText("0" + Integer.toString(key, 8));
        element = root.addElement("hyper").addText("&0x" + Integer.toString(key, 16));
       
    element = root.addElement("binary").addText(Integer.toString(key, 2) + "B");
        StringBuffer xmlDoc = null;

        StringWriter sw = new StringWriter();
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(sw, outformat);
        writer.write(document);
        writer.close();
        xmlDoc = sw.getBuffer();

        return xmlDoc.toString();
    }

    The dom4j library uses the static methodDocumentHelper.createDocument()to create the XML document. The methodroot.addElement()puts a child element on the root element, andaddText()puts the data in the elements. TheOutputFormatclass is then used to format theXMLDocument, so the document looks like this:

      <?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>

    This step can be skipped, because it only formats the document for readability by adding linefeeds and spaces. Since humans shouldn't need to read this document (unless you are debugging), you won't need the formatting.

    To use dom4j without the formatting, simply replace these two lines:

      OutputFormat outformat = OutputFormat.createPrettyPrint();
      XMLWriter writer = new XMLWriter(sw, outformat);

    with this line:

      XMLWriter writer = new XMLWriter(sw);

    Please check back next week for the continuation of 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 6 - Follow our Sitemap