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
System.out.println(doc.getRootElement().getName());
Element firstChild = (Element)doc.getRootElement().getChildren().get(0);
System.out.println(">> "+firstChild.getName()+":"+ firstChild.getAttribute("type").getValue());
System.out.println(">>>>" +firstChild.getText());
//easily print
this.printJDom(doc);
// do you want an classic DOM? no prob!
DOMOutputter out = new DOMOutputter();
org.w3c.dom.Document domDoc= out.output(doc);
System.out.println(domDoc.getImplementation().toString());
//lets print in the console the JDomTree
this.printJDom(doc);
//lets make some changes!
Element child =(Element) doc.getRootElement().getChildren().get(0);
child.setName("NewNAME");
child.setText("Water Mellon");
//lets print in the console the altered JDomTree
this.printJDom(doc);
} catch (JDOMException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* 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]));
}
}
- —–USE THIS SAMPLE XML IF YOU WISH———————
<?xml version="1.0"?>
<food>
<name type="fruit">Avocado Dip</name>
<mfr>Sunnydale</mfr>
<serving units="g">29</serving>
<calories total="110" fat="100"/>
<total-fat>11</total-fat>
<saturated-fat>3</saturated-fat>
<cholesterol>5</cholesterol>
<sodium>210</sodium>
<carb>2</carb>
<fiber>0</fiber>
<protein>1</protein>
<vitamins>
<a>0</a>
<c>0</c>
</vitamins>
<minerals>
<ca>0</ca>
<fe>0</fe>
</minerals>
</food>
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!
anon said
Have you looked at vtd-xml? it is a lot faster and memory efficient than DOM4J and JDOM
vtd-xml
webs review said
webs review…
[...]A Simple example using JDOM! « J for Java[...]…
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 –
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.
http://tinyurl.com/fblefoss20078 said
“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
sites like the pirate bay said
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.