Java and XML – Tutorial

Posted: February 28, 2012 in Java Posts
Tags: , , , , , , , , , , , , , , , ,

1. XML Introduction

1.1. XML Overview

XML stands for Extensible Markup Language and was defined 1998 by the World Wide Web Consortium (W3C).

A XML document consists out of elements, each element has a start tag, content and an end tag. A XML document must have exactly one root element, e.g. one tag which encloses the remaining tags. XML makes a difference between capital and non-capital letters.

A XML file is required to be well-formated.

Well-formated XML must apply to the following conditions:

  • A XML document always starts with a prolog (see below for an explanation of what a prolog is)
  • Every tag has a closing tag.
  • All tags are completely nested.


A XML file is valid if it is well-formated and if it is contains a link to a XML schema and is valid according to the schema.

In general the following is considered as advantages in using XML for data processing / representation.

  • XML is Plain text
  • XML allows the data identification without any display information
  • Style can be defined via XSL
  • Easily processed due to it regular and consistent notation
  • XML files are hierarchical


1.2. XML Example

The following is a valid, well-formated XML file.


<?xml version="1.0"?>
<!-- This is a comment -->
	<name>Lars </name>
	<street> Test </street>
	<telephon number= "0123"/>


1.3. XML Elements

1.3.1. Prolog

A XML document always starts with a prolog which describes XML file. This prolog can be minimal, e.g. <?xml version=”1.0″?> or can contain other information, e.g. the encoding, e.g. <?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes” ?>

1.3.2. Empty Tag

A tag which doesn’t enclose any content is know as an “empty tag”. For example: <flag/>

1.3.3. Commends

Comments in XML are defined as: <! COMMENT>

2. Java XML Overview

Java contains several methods to access XML. The following is a short overview of the available methods.

Java provides the DOM API (Document Object Model). In DOM your access the XML document over an object tree. DOM can be used to read and write XML files.

SAX (Simple API for XML) is an Java API to sequential reading of XML files. SAX can only read XML documents. SAX provides an Event-Driven XML Processing following the Push-Parsing Model. What this model means is that in SAX, Applications will register Listeners in the form of Handlers to the Parser and will get notified through Call-back methods. Here the SAX Parser takes the control over Application thread by Pushing Events to the Application. Both DOM and Sax are older API’s and I recommend not to use them anymore.

Stax (Streaming API for XML) is an API for reading and writing XML Documents. Introduced in Java 6.0 and considered as superior to SAX and DOM.

Java Architecture for XML Binding ( JAXB ) is a Java standard that defines how Java objects are converted to/from XML (specified using a standard set of mappings. JAXB defines a programmer API for reading and writing Java objects to / from XML documents and a service provider which / from from XML documents allows the selection of the JAXB implementation JAXB applies a lot of defaults thus making reading and writing of XML via Java very easy.

The following will explain the Stax interface, for an introduction into JAXB please see JAXB tutorial.

3. Streaming API for XML (StaX)

3.1. Overview

Streaming API for XML, called StaX, is an API for reading and writing XML Documents.

StaX is a Pull-Parsing model. Application can take the control over parsing the XML documents by pulling (taking) the events from the parser.

The core StaX API falls into two categories and they are listed below. They are

  • Cursor API
  • Event Iterator API


Applications can any of these two API for parsing XML documents. The following will focus on the event iterator API as I consider it more convenient to use.

3.2. Event Iterator API

The event iterator API has two main interfaces: XMLEventReader for parsing XML and XMLEventWriter for generating XML.

3.3. XMLEventReader – Read XML Example

This example is stored in project “de.vogella.xml.stax.reader”.

Applications loop over the entire document requesting for the Next Event. The Event Iterator API is implemented on top of Cursor API.

In this example we will read the following XML document and create objects from it. file.


<?xml version="1.0" encoding="UTF-8"?>
	<item date="January 2009">
	<item date="February 2009">
	<item date="December 2009">


Define therefore the following class to store the individual entries of the XML file.


package de.vogella.xml.stax.model;

public class Item {
	private String date; 
	private String mode;
	private String unit;
	private String current;
	private String interactive;

	public String getDate() {
		return date;

	public void setDate(String date) { = date;
	public String getMode() {
		return mode;
	public void setMode(String mode) {
		this.mode = mode;
	public String getUnit() {
		return unit;
	public void setUnit(String unit) {
		this.unit = unit;
	public String getCurrent() {
		return current;
	public void setCurrent(String current) {
		this.current = current;
	public String getInteractive() {
		return interactive;
	public void setInteractive(String interactive) {
		this.interactive = interactive;

	public String toString() {
		return "Item [current=" + current + ", date=" + date + ", interactive="
				+ interactive + ", mode=" + mode + ", unit=" + unit + "]";


The following reads the XML file and creates a List of object Items from the entries in the XML file.



import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


import de.vogella.xml.stax.model.Item;

public class StaXParser {
	static final String DATE = "date";
	static final String ITEM = "item";
	static final String MODE = "mode";
	static final String UNIT = "unit";
	static final String CURRENT = "current";
	static final String INTERACTIVE = "interactive";

	@SuppressWarnings({ "unchecked", "null" })
	public List<Item> readConfig(String configFile) {
		List<Item> items = new ArrayList<Item>();
		try {
			// First create a new XMLInputFactory
			XMLInputFactory inputFactory = XMLInputFactory.newInstance();
			// Setup a new eventReader
			InputStream in = new FileInputStream(configFile);
			XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
			// Read the XML document
			Item item = null;

			while (eventReader.hasNext()) {
				XMLEvent event = eventReader.nextEvent();

				if (event.isStartElement()) {
					StartElement startElement = event.asStartElement();
					// If we have a item element we create a new item
					if (startElement.getName().getLocalPart() == (ITEM)) {
						item = new Item();
						// We read the attributes from this tag and add the date
						// attribute to our object
						Iterator<Attribute> attributes = startElement
						while (attributes.hasNext()) {
							Attribute attribute =;
							if (attribute.getName().toString().equals(DATE)) {


					if (event.isStartElement()) {
						if (event.asStartElement().getName().getLocalPart()
								.equals(MODE)) {
							event = eventReader.nextEvent();
					if (event.asStartElement().getName().getLocalPart()
							.equals(UNIT)) {
						event = eventReader.nextEvent();

					if (event.asStartElement().getName().getLocalPart()
							.equals(CURRENT)) {
						event = eventReader.nextEvent();

					if (event.asStartElement().getName().getLocalPart()
							.equals(INTERACTIVE)) {
						event = eventReader.nextEvent();
				// If we reach the end of an item element we add it to the list
				if (event.isEndElement()) {
					EndElement endElement = event.asEndElement();
					if (endElement.getName().getLocalPart() == (ITEM)) {

		} catch (FileNotFoundException e) {
		} catch (XMLStreamException e) {
		return items;



You can test the parser via the following test program. Please note that the file config.xml must exist in the Java project folder.



import java.util.List;

import de.vogella.xml.stax.model.Item;

public class TestRead {
	public static void main(String args[]) {
		StaXParser read = new StaXParser();
		List<Item> readConfig = read.readConfig("config.xml");
		for (Item item : readConfig) {


3.4. Write XML File- Example

This example is stored in project “de.vogella.xml.stax.writer”.

Lets assume you would like to write the following simple XML file.


<?xml version="1.0" encoding="UTF-8"?>


StaX does not provide functionality to format the XML file automatically. So you have to add end-of-lines and tab information to your XML file.


package de.vogella.xml.stax.writer;



public class StaxWriter {
	private String configFile;

	public void setFile(String configFile) {
		this.configFile = configFile;

	public void saveConfig() throws Exception {
		// Create a XMLOutputFactory
		XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
		// Create XMLEventWriter
		XMLEventWriter eventWriter = outputFactory
				.createXMLEventWriter(new FileOutputStream(configFile));
		// Create a EventFactory
		XMLEventFactory eventFactory = XMLEventFactory.newInstance();
		XMLEvent end = eventFactory.createDTD("\n");
		// Create and write Start Tag
		StartDocument startDocument = eventFactory.createStartDocument();

		// Create config open tag
		StartElement configStartElement = eventFactory.createStartElement("",
				"", "config");
		// Write the different nodes
		createNode(eventWriter, "mode", "1");
		createNode(eventWriter, "unit", "901");
		createNode(eventWriter, "current", "0");
		createNode(eventWriter, "interactive", "0");

		eventWriter.add(eventFactory.createEndElement("", "", "config"));

	private void createNode(XMLEventWriter eventWriter, String name,
			String value) throws XMLStreamException {

		XMLEventFactory eventFactory = XMLEventFactory.newInstance();
		XMLEvent end = eventFactory.createDTD("\n");
		XMLEvent tab = eventFactory.createDTD("\t");
		// Create Start node
		StartElement sElement = eventFactory.createStartElement("", "", name);
		// Create Content
		Characters characters = eventFactory.createCharacters(value);
		// Create End node
		EndElement eElement = eventFactory.createEndElement("", "", name);




And a little test.


package de.vogella.xml.stax.writer;

public class TestWrite {

	public static void main(String[] args) {
		StaxWriter configFile = new StaxWriter();
		try {
		} catch (Exception e) {


For another (more complex example of using Stax please see Reading and creating RSS feeds via Java (with Stax)

4. XPath

4.1. Overview

XPath (XML Path Language) is a language for selecting / searching nodes from an XML document. Java 5 introduced the javax.xml.xpath package which provides a XPath library.

The following explains how to use XPath to query an XML document via Java.

4.2. Using XPath

The following explains how to use XPath. Create a new Java project called “UsingXPath”.

Create the following xml file.


<?xml version="1.0" encoding="UTF-8"?>


Create a new package “myxml” and a new Java class “QueryXML”.


package myxml;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class QueryXML {
	public void query() throws ParserConfigurationException, SAXException,
			IOException, XPathExpressionException {
		// Standard of reading a XML file
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		Document doc = null;
		XPathExpression expr = null;
		builder = factory.newDocumentBuilder();
		doc = builder.parse("person.xml");

		// Create a XPathFactory
		XPathFactory xFactory = XPathFactory.newInstance();

		// Create a XPath object
		XPath xpath = xFactory.newXPath();

		// Compile the XPath expression
		expr = xpath.compile("//person[firstname='Lars']/lastname/text()");
		// Run the query and get a nodeset
		Object result = expr.evaluate(doc, XPathConstants.NODESET);

		// Cast the result to a DOM NodeList
		NodeList nodes = (NodeList) result;
		for (int i=0; i<nodes.getLength();i++){

		// New XPath expression to get the number of people with name lars
		expr = xpath.compile("count(//person[firstname='Lars'])");
		// Run the query and get the number of nodes
		Double number = (Double) expr.evaluate(doc, XPathConstants.NUMBER);
		System.out.println("Number of objects " +number);

		// Do we have more then 2 people with name lars?
		expr = xpath.compile("count(//person[firstname='Lars']) >2");
		// Run the query and get the number of nodes
		Boolean check = (Boolean) expr.evaluate(doc, XPathConstants.BOOLEAN);


	public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
		QueryXML process = new QueryXML();


5. Thank you


  1. Ricki Arancibia says:

    Thanks for helping out, great info .

  2. etupakka says:

    I love your writing style really loving this website. “Take care of the luxuries and the necessities will take care of themselves.” by Dorothy Parker.

  3. certified nursing assistant exam says:

    Hello very cool web site!! Man .. Excellent .. Amazing .. I’ll bookmark your web site and take the feeds additionallyÖI’m satisfied to seek out so many useful information right here in the post, we need work out extra strategies on this regard, thank you for sharing.

  4. music licensing for films says:

    I got what you mean , thankyou for putting up.Woh I am pleased to find this website through google. “The eye of a human being is a microscope, which makes the world seem bigger than it really is.” by Kahlil Gibran.

  5. music publishing license says:

    I’ve recently started a blog, the info you provide on this website has helped me tremendously. Thanks for all of your time & work.

  6. teething necklace says:

    I love the efforts you have put in this, regards for all the great blog posts.

  7. Damion Hopps says:

    You have observed very interesting details! ps nice web site.

  8. film izle says:

    Seriously necessary article admin good one particular i bookmarked your word wide web web page see you in upcoming blog site publish.

  9. Broadjam scam says:

    Very interesting subject , appreciate it for putting up. “Nothing great was ever achieved without enthusiasm.” by George Ellis.

  10. hybrid cars says:

    Utterly pent written content , thankyou for selective information .

  11. music licensing deals says:

    Very interesting info !Perfect just what I was searching for!

  12. cheap vacation ideas says:

    Wow, marvelous blog format! How lengthy have you ever been running a blog for? you made running a blog look easy. The overall look of your website is magnificent, let alone the content!

  13. programacion television mexico says:

    Hello there! Would you mind if I share your blog with my twitter group? There’s a lot of people that I think would really appreciate your content. Please let me know. Thanks

  14. SEO packets says:

    I simply want to tell you that I am beginner to blogging and actually loved this blog site. Probably I will bookmark your website. You really come with great well crafted articles. Regards with regard to sharing with us the web-site.

  15. mixing mastering music says:

    I really like your writing style, good information, thanks for posting :D. “Your central self is totally untouched By grief, confusion, desperation.” by Vernon Howard.

  16. air sea travel honolulu says:

    fantastic issues altogether, you simply received a brand new reader. What might you recommend in regards to your publish that you simply made a few days in the past? Any positive?

  17. water lime rust says:

    The heart of your writing whilst sounding agreeable originally, did not really settle well with me after some time. Someplace within the paragraphs you actually managed to make me a believer unfortunately only for a very short while. I still have a problem with your jumps in logic and you might do well to help fill in all those breaks. When you can accomplish that, I will definitely end up being impressed.

  18. licensing music says:

    Land scape the moving earth and the lawns will follow on to the end days. Mowing down through Lawns care don’t eat the brown grass.

  19. Isaiah Sargetakis says:

    You are a very capable person!

  20. website translator says:

    Its great as your other posts : D, thanks for posting . “What makes something special is not just what you have to gain, but what you feel there is to lose.” by Andre Agassi.

  21. accelerated nursing programs says:

    Good day very cool web site!! Guy .. Beautiful .. Wonderful .. I’ll bookmark your web site and take the feeds alsoÖI am glad to find a lot of useful info right here in the put up, we need develop extra techniques on this regard, thanks for sharing.

  22. eliminating credit card bills says:

    I do trust all the concepts you have presented for your post. They are really convincing and will definitely work. Nonetheless, the posts are very short for starters. Could you please lengthen them a little from next time? Thank you for the post.

  23. Larraine says:

    Its fantastic as your other blog posts : D, thanks for putting up. “History is a pact between the dead, the living, and the yet unborn.” by Edmund Burke.

  24. arlington basketball league says:

    I genuinely enjoy examining on this internet site , it has got superb blog posts.

  25. four micro onde multifonction says:

    you are really a just right webmaster. The site loading velocity is amazing. It sort of feels that you are doing any distinctive trick. Also, The contents are masterpiece. you have done a wonderful process in this topic!

  26. television sets says:

    My sister saved this internet site to me and that i are actually experiencing it in the past several hrs. This is definitely going to benefit me and my classmates for the class project. Moreover, I’m keen on like you would write.

  27. credit card holder says:

    It’s in point of fact a nice and helpful piece of info. I’m happy that you simply shared this helpful info with us. Please keep us informed like this. Thank you for sharing.

  28. I used to be curious about your up coming put up admin really wanted this blog super amazing website

  29. URL Indexer says:

    You are my breathing in, I have few blogs and often run out from to post .

  30. Eva Angelina says:

    Thankyou for helping out, fantastic information.

  31. William says:

    I always was interested in this subject and stock still am, thank you for putting up.

  32. Scott Revel says:

    Im grateful for that blog. Many thanks Once again. Excellent.

  33. Patrick Pulk says:

    Magnificent goods from you, man. I’ve understand your stuff previous to and you’re just extremely fantastic. I actually like what you’ve acquired here, really like what you are stating and the way in which you say it. You make it entertaining and you still care for to keep it wise. I can’t wait to read far more from you. This is really a wonderful website.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s