J for Java

Tips and thoughts around Java!

A Simple example using JDOM!

Posted by javaneze on June 9, 2006

For years I have been using the classic DOM implementations using Xerces, etc. Lots of people claim that the DOM API in Java is a bit strange, I always thought that the Java XML APIs in general were quite clean and easy to learn.Well anyway. I have not used JDOM since now! Manipulating DOM trees and making changes could not have been easier! So here is some sample code that illustrates JDOM's basic functions (the API has lots of things). Its for beginners I suppose!

Have fun with JDOM!


 * PlayWithJDom.java
 * @author javaneze@gmail.com

package jforjava;

import java.io.File;
import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.DOMOutputter;
import org.jdom.output.XMLOutputter;

public class PlayWithJDom {

XMLOutputter printer = null;

public PlayWithJDom() {
        //lets create an XML Printer!
        printer = new XMLOutputter();

     * Parsing an XML file and then
     * playing with some basic JDOM functionality!
     * @param anXmlDocFile
    public void parseAndPlay(File anXmlDocFile){
        //create a parser
        SAXBuilder parser = new SAXBuilder();
        try {
            //get the dom-document
            Document doc = parser.build(anXmlDocFile);

// illustrate some jdom tree walk and print sample
            Element firstChild = (Element)doc.getRootElement().getChildren().get(0);
            System.out.println(">> "+firstChild.getName()+":"+ firstChild.getAttribute("type").getValue());
            System.out.println(">>>>" +firstChild.getText());

//easily print

// do you want an classic DOM? no prob!
            DOMOutputter out = new DOMOutputter();
            org.w3c.dom.Document domDoc= out.output(doc);

//lets print in the console the JDomTree

//lets make some changes!
            Element child =(Element) doc.getRootElement().getChildren().get(0);
            child.setText("Water Mellon");

//lets print in the console the altered JDomTree

} catch (JDOMException ex) {
        } catch (IOException ex) {

     * Using the XMLOuputer prints in the Console
     * the JDOM tree!
     * @param aJDOMdoc A JDOM Document
     * @throws java.io.IOException
    public void printJDom(Document aJDOMdoc) throws IOException{
        System.out.println("<------------XML DOCUMENT------------>");
        this.printer.output(aJDOMdoc, System.out);

     * Main Method - Program Start!
     * @param args
    public static void main(String[] args){
        PlayWithJDom play = new PlayWithJDom();
        play.parseAndPlay(new File(args[0]));

<?xml version="1.0"?>
<name type="fruit">Avocado Dip</name>
<serving units="g">29</serving>
<calories total="110" fat="100"/>

Posted in tips | 11 Comments »

J for Java

Posted by javaneze on June 8, 2006

My Java persona wishes to have its own site its own blog! My trips to java world and random thoughts about various technologies I encounter during my free java time or during my work.



Posted in random thoughts | 7 Comments »