Java XML – JDOM2 – SAXBuilder Example

In this tutorial we look at how to build a JDOM2 using a SAXBuilder. To understand how SAXBuilder works and how to configure it look at this tutorial. The example below demonstrates the following

  • Building a JDOM2 document using a SAXBuilder.
  • Obtaining the DOCTYPE of the document.
  • Obtaining the root element of the document
  • Obtaining a specific child (specify name) of an element
  • Iterating through the descendants of an Element and printing the values of all elements whose type is not Text or Comment
  • Iterating through the descendants of an Element and printing the elements of type Comment (using a Comment filter)

The example below uses the tomcat web.xml file which can be downloaded from
here

package com.studytrails.xml.jdom;

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

import org.jdom2.Comment;
import org.jdom2.Content;
import org.jdom2.Content.CType;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;
import org.jdom2.util.IteratorIterable;

public class JdomUsingSAXBuilder {

	private static String file1 = "tomcat-web-dtd.xml";

	public static void main(String[] args) throws JDOMException, IOException {

		// Use a SAX builder
		SAXBuilder builder = new SAXBuilder();
		// build a JDOM2 Document using the SAXBuilder.
		Document jdomDoc = builder.build(new File(file1));

		// get the document type
		System.out.println(jdomDoc.getDocType());

		//get the root element
		Element web_app = jdomDoc.getRootElement();
		System.out.println(web_app.getName());
		
		// get the first child with the name 'servlet'
		Element servlet = web_app.getChild("servlet");

		// iterate through the descendants and print non-Text and non-Comment values
		IteratorIterable<Content> contents = web_app.getDescendants();
		while (contents.hasNext()) {
			Content web_app_content = contents.next();
			if (!web_app_content.getCType().equals(CType.Text) && !web_app_content.getCType().equals(CType.Comment)) {
				System.out.println(web_app_content.toString());
			}
		}

		// get comments using a Comment filter
		IteratorIterable<Comment> comments = web_app.getDescendants(Filters.comment());
		while (comments.hasNext()) {
			Comment comment = comments.next();
			System.out.println(comment);
		}

	}
}


2 thoughts on “Java XML – JDOM2 – SAXBuilder Example”

  1. This example is working one, but the xml which here described is wrong consist errors, but hura i was able to parse it with a given Java example

    Reply

Leave a Reply to Alexander Cancel reply