XML: An Introduction




* HTML stands for Hyper Text Markup Language

* An HTML file is a text file containing small markup tags

* The markup tags tell the Web browser how to display the page

* An HTML file must have an htm or html file extension

* An HTML file can be created using a simple text editor

from http://www.w3schools.com/html/html_intro.asp

Here's an example :

                <title>Web Page</title>
                <strong>Hello</strong>, this is a web page!
                <font color=red>This is some red text</font>




* XML stands for EXtensible Markup Language

* XML is a markup language much like HTML

* XML was designed to describe data

* XML tags are not predefined. You must define your own tags

* XML uses a Document Type Definition (DTD) or an XML Schema to describe the data

* XML with a DTD or XML Schema is designed to be self-descriptive

* XML is a W3C Recommendation

from http://www.w3schools.com/xml/xml_whatis.asp

If you use an RSS reader to access blogs or other newsfeeds, you use XML. Really Simple Syndication produces XML-based feeds summarizing frequently updated content.

Here's a real example from nytimes.com:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
                <title>NYT > Sunday Book Review</title>
                <copyright>Copyright 2007 The New York Times Company</copyright>
                <lastBuildDate>Fri, 24 Aug 2007 20:05:02 GMT</lastBuildDate>
                        <title>NYT > Sunday Book Review</title>
                        <title>On the Road Again</title>
                        <description>The novel that &#8220;On the Road&#8221; became was inarguably the book that young people needed in 1957, but the sparse and unassuming scroll is the living version for our time.</description>
                        <author>LUC SANTE</author>
                        <guid isPermaLink="false">http://www.nytimes.com/2007/08/19/books/review/Sante2-t-1.html</guid>
                        <pubDate>Sun, 19 Aug 2007 02:56:43 GMT</pubDate>


Elements are surrounded by tags. Tags come in pairs. The open tag identifies the beginning of the element and the closing tag, denoted by the / before the tag name, identifies the end of the element. Tag names are generally fairly intuitive descriptions of the data that will be contained in the element. For example, as you might expect, the text between the author tags is the name of an author. In some cases, you may find empty elements that look as follows: <description/>.

Elements may contain text, other elements, and attributes (discussed below). In the example above, the rss element contains one element, channel. The channel element contains elements title, link, description, language, copyright, lastBuildData, image, and item.

Elements form a tree structure or hierarchy. Following is some relevant tree terminology:


Attributes are name, value pairs that provide some information about the characteristics of an element. In the example above, the element rss has an attribute version. The version attribute has a value of 2.0. An element may have multiple attributes.


The prolog identifies a document as an XML document and may contain other relevant information. An example document with prolog follows:

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

<!DOCTYPE greeting SYSTEM "hello.dtd">

<greeting>Hello, world!</greeting>

The first line identifies the document as an XML document and specifies the encoding. The second line is the document type declaration. It indicates that the root element is a greeting. The document type definition for a greeting document is found in "hello.dtd".


An XML document must be well formed. A well-formed document has the following properties:


There are two models for parsing XML: DOM and SAX.

DOM - Document Object Model

A DOM parser reads an XML document, for example from a file, and builds a tree in memory. The programmer can then access and manipulate the information stored in the document by traversing the tree structure. Essentially, the job of the parser is to identify where elements start and end, and build objects to represent each element.

SAX - Simple API for XML

A SAX parser reads an XML document and generates events when elements are found. The user defines the actions be taken as different types of elements are found.

XML and Java

XMLTester.java - a very simple example

Java provides both DOM and SAX parsers in the javax.xml.parsers package. The DOM parser produces a Document object, where Document is in the org.w3c.dom package. The Document represents the entire XML tree, which is comprised of Node objects. The Node class provides an API to traverse the tree. Node has several subclasses, the most notable of which are Text and Element. All components in the tree are Nodes, but some are Elements and some are Text, and there are a few other subclasses as well. Below are a few of the most relevant APIs. For a full listing, see the Java API.


DocumentBuilderFactory - Defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents.

DocumentBuilder - Defines the API to obtain DOM Document instances from an XML document. Using this class, an application programmer can obtain a Document from XML.







The standard API provides minimal support for finding nodes. You can, for example, retrieve all elements with a particular tag name. However, if you wanted to retrieve all elements with a particular value you would have to traverse the entire structure to find the element you wanted. XPath is a mechanism you can use to specify a pattern to represent a node or set of nodes. You apply the XPath expression to a DOM tree and the result is a node or node list.

An XPath expression can select a node or set of nodes. The expression uses a location path, which is a series of location steps. A step specifies the following:

Example XPath expressions:

The javax.xml.xpath package provides support for evaluating XPath expressions in Java.

XPathTester.java - a simple example using XPath

XML Schema/DTDs

Suppose you want to write an RSS client application that downloads RSS feeds and displays them for the user. It would certainly be helpful to know what the RSS feeds will look like, for example that each item element will have a subelement title. Document Type Definitions (DTD) and XML Schema are two mechanisms for specifying the structure of a particular class of XML documents.


DTDs are an older mechanism for specifying schema. DTDs allow you to specify the set of allowable elements, how they fit together, and the legal values that can be assigned to them. Here is a sample RSS DTD.

XML Schema

XML Schema is a newer and more powerful way to specify the schema for a particular class of XML document. XML Schema uses XML, so a standard XML parser can parse an XML Schema. In addition, XML Schema provides more powerful mechanisms to enable you to specify the order of elements, types of data, etc. Here is a sample RSS XSD.

Schema Datatypes

XML Schema allows the programmer to specify the type for a particular element. Example types include the following:

Complex Types

Complex types specify compositions of simple types. For example, the schema for the following document must specify that a date element is composed of a month element, a day element, and a year element:


The schema for this document would look as follows:

<xs:element name="date">
    <xs:element ref="year"/>
    <xs:element ref="month"/>
    <xs:element ref="day"/>
<xs:element name="year" type="xs:integer"/>
<xs:element name="month" type="xs:integer"/>
<xs:element name="day" type="xs:integer"/>

Value Restrictions

You can also use the simpleType to restrict the values for a particular element. For example, you can restrict the month element to a number between 1 and 12 as follows:

<xs:simpleType name="monthNum">
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="1"/>
    <xs:maxInclusive value="12"/>
<xs:element name="month" type="monthNum"/>

Or, you can specify the pattern that an element must follow using a regular expression:

<xs:element name="price" type="priceval" 
<xs:simpleType name="priceval"> 
  <xs:restriction base="xs:token"> 
    <xs:pattern value="[0-9]+[0-9]2"/> 

Or, you can use an enumeration to define the types that an element can take:

<xs:simpleType name="genderType"> 
  <xs:restriction base="xs:token"> 
    <xs:enumeration value="female"/> 
    <xs:enumeration value="male"/> 


There are several ways to group elements:

Referencing a Schema

  period="P3M" periodEnding="1999-12-31">

More Examples


If you take a look at the NPR Story of the Day, you'll notice that the XML looks a bit different than the previous RSS feed.

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet title="XSL_formatting" type="text/xsl" href="/include/xsl/podcast.xsl"?>
<rss version="2.0" xmlns:npr="http://www.npr.org/rss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:content="http://purl.org/rss/1.0/modules/content/"> 
                <title>NPR: Story of the Day</title>
                <description>Funny, moving, exceptional, or just offbeat -- the NPR story people will be talking about tomorrow. The best of Morning Edition, All Things Considered and other award-winning NPR programs.</description>  
                <copyright>Copyright 2007 NPR - For Personal Use Only</copyright>  
                <generator>NPR/RSS Generator 2.0</generator>  
                <lastBuildDate>Thu, 30 Aug 2007 01:06:17 EDT</lastBuildDate>  
                <itunes:summary>Funny, moving, exceptional, or just offbeat -- the NPR story people will be talking about tomorrow. The best of Morning Edition, All Things Considered and other award-winning NPR programs.</itunes:summary>  
                <itunes:subtitle>Editors&apos; Pick. The best of Morning Edition, All Things Considered and other award-winning NPR programs.</itunes:subtitle>  
                <itunes:author>National Public Radio</itunes:author>  
                <itunes:keywords>story,of,the,day,NPR,National Public Radio,Story of the Day,Morning Edition,All Things Considered,Fresh Air</itunes:keywords>  
                        <title>Story of the Day</title>   
                <itunes:category text="Arts"/>  
                <itunes:category text="Society &amp; Culture"/>  
                <itunes:image href="http://media.npr.org/images/podcasts/primary/npr_sotd_image_300.jpg"/>  
                        <title>New Orleans Suffers Crisis in Mental Health Care</title>   
                        <description>Two years after Hurricane Katrina, many New Orleans residents need mental health care, but there are few resources and almost no psychiatric beds. With nowhere to turn, people in the city have been forced to take drastic steps.</description>   
                        <pubDate>Thu, 30 Aug 2007 01:06:08 EDT</pubDate>   
                        <itunes:summary>Two years after Hurricane Katrina, many New Orleans residents need mental health care, but there are few resources and almost no psychiatric beds. With nowhere to turn, people in the city have been forced to take drastic steps.</itunes:summary>   
                        <itunes:keywords>NPR,National Public Radio,New Orleans Suffers Crisis in Mental Health Care,</itunes:keywords>   
                        <enclosure url="http://podcastdownload.npr.org/anon.npr-podcasts/podcast/1090/14042689/npr_14042689.mp3" length="6456767" type="audio/mpeg"/>  

Among other things, you see a set of tags that have the prefix itunes. As you might imagine, the elements with tags beginning with itunes provide information that can be used by the iTunes program when it processes the feed. A standard RSS reader can process this same feed, but may ignore any elements with tags in the itunes namespace.

Essentially, namespaces enable you to create a document that conforms to multiple schemas. Each schema has its own namespace and element names are preceeded with the namespace. This avoids conflicts between two namespaces. The web page: http://www.feedforall.com/directory-namespace.htm lists some other common namespaces.

Sami Rollins
Wednesday, 07-Jan-2009 15:13:20 PST