Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

Java and XML - Tutorial

Lars Vogel

Version 1.1

08.12.2009

Revision History
Revision 0.626.02.2008Lars Vogel
Sax, Dom, Stax
Revision 0.721.10.2008Lars Vogel / Marcus Rieck
Added Jaxb
Revision 0.7.120.02.2009Lars Vogel
Corrected typo in intro XML example
Revision 0.7.220.04.2009Lars Vogel
Fixed incorrect package and setter in JAXB example
Revision 0.821.07.2009Lars Vogel
Valid XML
Revision 0.905.11.2009Lars Vogel
Improved Stax reader example with nestled XML elements
Revision 1.006.11.2009Lars Vogel
Added link to RSS article
Revision 1.108.12.2009Lars Vogel
fixed example XML (comment was not closed)

Java and XML

This article give an introduction to XML and its usage with Java. The Java Streaming API for XML (Stax), JAXB 2 and the Java XPath library are explained and demonstrated.

This article is based on Java 6.0.


Table of Contents

1. XML Introduction
1.1. XML Overview
1.2. XML Example
1.3. XML Elements
2. Java XML Overview
2.1. DOM (Document Object Model)
2.2. SAX (Simple API for XML)
2.3. Stax (Streaming API for XML)
2.4. Java Architecture for XML Binding (JAXB)
3. Streaming API for XML (StaX)
3.1. Overview
3.2. Event Iterator API
3.3. XMLEventReader - Read XML Example
3.4. Write XML File- Example
4. JAXB 2 - Java Architecture for XML Binding
4.1. Overview
4.2. Usage
5. XPath
5.1. Overview
5.2. Using XPath
6. Thank you
7. Questions and Discussion
8. Links and Literature
8.1. Source Code
8.2. Links and Literature
8.3. vogella Resources

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 -->
<address>
	<name>Lars </name>
	<street> Test </street>
	<telephon number= "0123"/>
</address>
			

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>