Java Video Tutorial 46

JDOM2 Video TutorialToday I introduce the easiest way to read and write to XML files using Java. It is call the JDOM2 library.Β With it you’ll easily read any element, or attribute and create your own.

I introduce 10 libraries and show you numerous methods that will make your life easier in just 14 minutes!

All of the code follows the video to improve the learning process.

If you like videos like this, please tell Google [googleplusone]

Sharing is always appreciated

Code from the Video

// Class that will represent a system file name


// Used to write data to a file


// Triggered when an I/O error occurs


// Represents your XML document and contains useful methods

import org.jdom2.Document;

// Represents XML elements and contains useful methods

import org.jdom2.Element;

// Top level JDOM exception

import org.jdom2.JDOMException;

// Represents text used with JDOM

import org.jdom2.Text;

// Creates a JDOM document parsed using SAX Simple API for XML

import org.jdom2.input.SAXBuilder;

// Formats how the XML document will look

import org.jdom2.output.Format;

// Outputs the JDOM document to a file

import org.jdom2.output.XMLOutputter;
public class Lesson46 {
	public static void main(String[] args) {
		// writeXML();
	private static void readXML(){
		SAXBuilder builder = new SAXBuilder();
		try {
			// Parses the file supplied into a JDOM document
			Document readDoc = File("./src/jdomMade.xml"));
			// Returns the root element for the document
			System.out.println("Root: " + readDoc.getRootElement());
			// Gets the text found between the name tags
			System.out.println("Show: " + readDoc.getRootElement().getChild("show").getChildText("name"));
			// Gets the attribute value for show_id assigned to name
			System.out.println("Show ID: " + readDoc.getRootElement().getChild("show").getChild("name").getAttributeValue("show_id") + "\n");
			Element root = readDoc.getRootElement();
			// Cycles through all of the children in show and prints them
			for (Element curEle : root.getChildren("show")) {
			  System.out.println("Show Name: " + curEle.getChildText("name"));
			  System.out.println("Show ID: " + curEle.getChild("name").getAttributeValue("show_id"));
			  System.out.print("On " + curEle.getChildText("network") + " in the ");
			  System.out.println(curEle.getChild("network").getAttributeValue("country") + "\n");
		catch (JDOMException e) {
		catch (IOException e) {
			// TODO Auto-generated catch block
	private static void writeXML(){
		// Creates a JDOM document
		Document doc = new Document();
		// Creates a element named tvshows and makes it the root
		Element theRoot = new Element("tvshows");
		// Creates elements show and name
		Element show = new Element("show");
		Element name = new Element("name");
		// Assigns an attribute to name and gives it a value
		name.setAttribute("show_id", "show_001");
		// Adds text between the name tags
		name.addContent(new Text("Life On Mars"));
		Element network = new Element("network");
		network.addContent(new Text("ABC"));
		network.setAttribute("country", "US");
		// Adds name and network to the show tag
		// Adds the show tag and all of its children to the root
		// Add a new show element like above
		Element show2 = new Element("show");
		Element name2 = new Element("name");
		name2.setAttribute("show_id", "show_002");
		name2.addContent(new Text("Freaks and Geeks"));
		Element network2 = new Element("network");
		network2.addContent(new Text("ABC"));
		network2.setAttribute("country", "US");
		// Uses indenting with pretty format
		XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
		// Create a new file and write XML to it
		xmlOutput.output(doc, new FileOutputStream(new File("./src/jdomMade.xml")));
		System.out.println("Wrote to file");
		catch (Exception e) {

/* Installing JDOM 
 * Download jdom.jar
 * Download
 * Right click default eclipse directory - Build Path
 * Click Java Build Path
 * Click Libraries tab - external libraries
 * Add the JDOM libraries
 * */

7 Responses to “Java Video Tutorial 46”

  1. Joel says:


    Very, very nice Tutorial. I did not know anything of that. But now I can write and read XML easily.
    One simple Question: When I want to write for example the country more than once (with different content), do I have to define a new Element or could I use the created one?

    answer at mail please πŸ™‚

    Best regards,

    • admin says:

      You’re very welcome πŸ™‚ You don’t need to create a separate element, but instead use the one that is already there. Experiment a bit and you’ll really get the concept

  2. Maggi says:

    Hi Derek, how it’s different from dom parsing ?? I feel there is no difference then why jdom ?? Can you throw some light on this ?

  3. Hi Derek, this is an off-topic question, but still I want to ask. In your videos I see how quickly you type/replace/fill in some things, I do know about ctrl+space combination, but do you use any other tricks? Or it is just you so dexterous ? πŸ™‚ And what do you think of making tutorial on that, if there’s any trick of course?

Leave a Reply to Joel Cancel reply

Your email address will not be published.