Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

Java and XML - Tutorial

Lars Vogel

Version 1.3

08.03.2011

Revision History
Revision 0.126.02.2008Lars Vogel
created
Revision 0.2 - 1.221.10.2008 - 08.03.2011Lars Vogel
bug fixes and enhancements

Java and XML

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


Table of Contents

1. XML Introduction
1.1. XML Overview
1.2. XML Example
1.3. XML Elements
2. Java XML Overview
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. XPath
4.1. Overview
4.2. Using XPath
5. Thank you
6. Questions and Discussion
7. Links and Literature
7.1. Source Code
7.2. Links and Literature
7.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>