Today was a good day –I preached to an acquaintance about JDOM [1] and why I decided to use it in preference to other XML APIs [2, 3]. In the process, I realised I had not blogged about it. I am hoping this short article will help someone get started with XML and Java… programming is fun and believe me you will have fun using JDOM.
What is JDOM?
– Simply put, JDOM is a way to represent an XML document for easy and efficient reading, manipulation, and writing [7].
– It is an awesome Java api and is quite fun to play with.
Why did I choose JDOM over the other APIs?
– I am pressed with time and needed something to quickly help me get up to speed.
– It is a robust and mature solution –Jason founded the JDOM project in early 2000 along with Brett McLaughlin
– It was accepted as JSR 102 [4], but later withdrawn by lead developers due to perceived complexities [5] that would arise if it was to be bundled with the JDK.
– There is a slew of resources online… stackoverflow.com has especially been reliable source of information. Most importantly though, there are dedicated mailing lists that are very active [6].
How does it work?
Like any third-party java library, one needs to download it and add it the class path. A tool like Maven comes in handy, especially when used and an integrated component with an IDE like Eclipse [8] –I love eclipse and I use it for most of my development.
Examples
Source File
<?xml version="1.0" encoding="UTF-8"?> <javaapi> <jdom> <name>JDOM</name> <url>http://www.jdom.org/</url> </jdom> <jaxp> <name>JAXP</name> <url>http://jaxp.java.net/</url> </jaxp> <dom4j> <name>dom4j</name> <url>http://dom4j.sourceforge.net/</url> </dom4j> </javaapi>
1. Reading an XML File
*** Sourcecode ***
package org.lightonphiri.jdom; import java.io.File; import java.io.IOException; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; public class JDOMReader { public static void main(String[] args) { SAXBuilder builder = new SAXBuilder(); try { Document document = builder.build(new File("jdom_xml_file.xml")); Element rootNode = document.getRootElement(); for(Element x : rootNode.getChildren()) { System.out.println(x.getName()); for (Element y : x.getChildren()) { System.out.println(y.getName() + " : " + y.getText()); } } } catch(IOException ioe) { ioe.printStackTrace(); } catch(JDOMException jdome) { jdome.printStackTrace(); } } }
*** Output ***
jdom name : JDOM url : http://www.jdom.org/ jaxp name : JAXP url : http://jaxp.java.net/ dom4j name : dom4j url : http://dom4j.sourceforge.net/
2. Write to an XML File
package org.lightonphiri.jdom; import java.io.FileWriter; import java.io.IOException; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; public class JDOMWriter { public static void main(String[] args) { // create child nodes and corresponding content Element name = new Element("name"); name.addContent("XOM"); Element url = new Element("url"); url.addContent("http://xom.nu/"); // add child nodes Element xom = new Element("xom"); xom.addContent(name); xom.addContent(url); // create document object SAXBuilder builder = new SAXBuilder(); try { Document document = builder.build("jdom_xml_file.xml"); document.getRootElement().addContent(xom); XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat()); xmlOutput.output(document, System.out); // output new structure xmlOutput.output(document, new FileWriter("jdom_xml_file_new.xml")); } catch(IOException ioe) { ioe.printStackTrace(); } catch(JDOMException jdome) { jdome.printStackTrace(); } } }
Where do I go from here?
- JDOM 2 A Primer [9] is possibly the best place to start from. It has neat pointers and examples on how you can exploit the API.
- The JDOM 2.0 javadocs documentation [10] will be very helpful –especially once you get up to speed with using the API.
- StackOverflow has tonnes of questions on JDOM –a quick search normally reveals helpful solutions to common problems.
- Subscribe to the mailing lists [6] –the people on the lists are more than willing to help.
I hope this helps someone out there.
Bibliography
[1] http://www.jdom.org
[2] http://stackoverflow.com/questions/373833/best-xml-parser-for-java
[3] http://docs.oracle.com/javase/tutorial/jaxp/index.html
[4] http://jcp.org/en/jsr/detail?id=102
[5] http://cvs.jdom.org/pipermail/jdom-interest/2004-September/014191.html
[6] http://www.jdom.org/involved/lists.html
[7] http://www.jdom.org/docs/faq.html#a0000
[8] http://www.eclipse.org/
[9] https://github.com/hunterhacker/jdom/wiki/JDOM2-A-Primer
[10] http://www.jdom.org/docs/apidocs/