Friday, December 30, 2011

Create XML document using JDOM example


Here's an example to create an XML document using JDOM API. JDOM provides an easy way for manipulating XML documents. 

To download the latest JDOM API, please visit here.

The following xml file "library.xml" will be created at the end ;

<?xml version="1.0" encoding="UTF-8"?>
<Library>
     <!--Description of books-->
     <book id="A111">
         <title>Processing XML with Java</title>
         <author>Elliotte Rusty Harold</author>
         <isbn><![CDATA[123-45-678-3832-6]]></isbn>
         <price>55</price>
     </book>
     <book id="A150">
         <title>Java How To Program</title>
         <author>Deitel & Deitel</author>
         <isbn><![CDATA[978-81-203-3832-6]]></isbn>
         <price>70</price>
     </book>
</Library>



Source Code : 


package com.manoj.examples.xml;

import java.io.FileWriter;
import java.io.IOException;

import org.jdom.CDATA;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 * The Class XMLCreator.
 * @author Manoj Shrestha
 * @contact m.javaprogrammer@gmail.com
 */

public class XMLCreator {
     public static void main(String[] args) {
           // defining root element
           Element rootElement = new Element("Library");
           Document document = new Document(rootElement);

           // defining parent element "book"
           Element book = new Element("book");
           // defining the child elements of "book"
           Element title = new Element("title");
               title.addContent("Processing XML with Java");
           Element author = new Element("author");
               author.addContent("Elliotte Rusty Harold");
           Element isbn = new Element("isbn");
               isbn.addContent(new CDATA("123-45-678-3832-6"));
           Element price = new Element("price");
               price.addContent("55");

           // adding a comment
           rootElement.addContent(new Comment("Description of books"));

           // adding attribute to parent element "book"
           book.setAttribute("id", "A111");
           // adding child elements to parent "book" element
           book.addContent(title);
           book.addContent(author);
           book.addContent(isbn);
           book.addContent(price);

           // adding element "book" to root element
           rootElement.addContent(book);

           /**
            * the nodes can also be added in cascaded style here, another parent
            * element "book" is added to the root element in cascaded style
            *
            * */
           Element book2 = new Element("book");
           book2.setAttribute("id", "A150");
           book2.addContent(new Element("title").addContent("Java How To Program"));
           book2.addContent(new Element("author").addContent("Deitel & Deitel"));
           book2.addContent(new Element("isbn").addContent(new CDATA(
                     "978-81-203-3832-6")));
           book2.addContent(new Element("price").addContent("70"));

           // adding second element "book" to root element
           rootElement.addContent(book2);

           // displaying the XML document in console output
           try {
                XMLOutputter outputter = new XMLOutputter();
                outputter.setFormat(Format.getPrettyFormat());
                outputter.output(document, System.out);

           } catch (IOException e) {
                e.printStackTrace();
           }

           // saving the XML document in a file
           try {
                FileWriter writer = new FileWriter("library.xml");
                XMLOutputter outputter = new XMLOutputter();
                outputter.setFormat(Format.getPrettyFormat());
                outputter.output(document, writer);
                writer.close();
           } catch (IOException e) {
                e.printStackTrace();
           }
     }
}

For further detail processing of XML documents in java, you can visit here.

No comments :

Post a Comment