J for Java

Tips and thoughts around Java!

  • Categories

  • Archives

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"/>


11 Responses to “A Simple example using JDOM!”

  1. William said

    Thanks allot for the above examples. I have done a fair amount with creating and writing XML with JDOM. But still doing most parsing via w3c stuff. Which lacks like the pretty format. Which I prefer when debugging stuff. Instead of all xml as a stream.

    Thanks again, booked marked as a reference!

  2. anon said

    Have you looked at vtd-xml? it is a lot faster and memory efficient than DOM4J and JDOM


  3. webs review…

    […]A Simple example using JDOM! « J for Java[…]…

  4. Yogesh Patil said

    can you help me in parsing xml with nested xml opening tags . when i am trying to parse this xml, i m getting parsing error.
    XML –

  5. test.com said

    Hey I know this is off topic but I was wondering if
    you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  6. “A Simple example using JDOM! J for Java” was indeed
    a good read and thus I was extremely happy to come across
    the article. Thanks a lot-Moises

  7. I have experienced a robust sober man driven perfectly mad for now by two servings of
    so-called rum, supplied to him at one of these
    simple shanties. They had arranged a few signals between themselves.
    Finding the Venus reef abandoned he went towards the Four
    Mile, two miles distant, and located about fourteen Chinamen camped there.

  8. Movies of every genre can be obtained from Buried and
    Prowl in horror to gems like No Strings Attached in romantic comedies.
    At their finest they produce moments that produce life worth living.
    This is most certainly not the very first case of any virus packaged as being a particular anti-virus program, along with the sad info is it isn’t prone to end up the last.

  9. You can observe the latest weather updates by just taking a look into your phone’s home screen, where it automatically posts weather info, and many types of without having to launch the application. A very convenient strategy to search for things around the web is to merely speak your pursuit term. The free Lyrics Search app is available at the Android Market.

  10. My partner and I stumbled over here by a different web page and thought
    I may as well check things out. I like what I see so i am just following you.
    Look forward to checking out your web page repeatedly.

  11. Het boek is echt nog pakken beter dan de film, en vooral de humor van het personage is in het boek nog tien keren meer uitvergroot, dus volgens mij ga Click https://twitter.com/moooker1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: