With all the XML hype going on I thought it would be a good idea if I looked into XML to see
if I was missing anything. To my frustration I found a lot of articles that explained the theory of XML and what
it could do for you, but found few code examples to get me started. This series of tutorials hopes to solve that. Within
the next several weeks I will walk you through several tutorials on how to use XML in Java. I will not spend too much
time on the theory of XML, rather these tutorials will focus primarily on coding and implementation. Ready? Lets get started!
What is XML?
As promised I'll keep this brief trying to keep these tutorials more about implementation
rather than theory. I think the easiest way to answer this question
is to present some sample XML.
<?xml version="1.0"?>
<order>
<item>
<name>Soccer Ball</name>
<price>15.00</price>
<quantity>5</quantity>
</item>
</order>
Looks like HTML, Right? Sure it looks like HTML but in reality it is something completely different.
HTML is a markup language used to format content for display on our screen. HTML uses a series of
predefined HTML tags to represent fonts, paragraph breaks, tables and so on. XML on the other hand is a language
used to describe the content, using tags which are defined by someone who understand how the document should be structured.
Looking at the XML document above we can easily recognize that this XML document
describes and order which contains an item. The item ordered is further defined as having
name, price and quantity elements. So XML really describes the content while HTML is for formatting content.
That wasn't so hard was it? However the real meat of this tutorial is explaining how we can actually extract information
from an XML document. Let's get started shall we.
First of all, when reading an XML document we have to decide on which XML parser to use.
There are two types of parsers available, SAX (Simple API for XML) and
DOM (Document Object Model). SAX is an event driven XML parser. It performs much like the current event handling model in Java.
For example in an applet just as you would register an event handler to process mouse clicks, in SAX you would register
an event handler to handle the begin and end tags of an element i.e. <name> and </name>.
Since most of us are familiar with event handling in Java
lets start off using SAX saving DOM for next time. For this and other XML tutorials I will be using XML parsers provided by IBM. These are free to
use and open source.
Download XML Parser for Java Unzip the file and add the included .jar files
to your CLASSPATH.
To parse an XML document using SAX we will perform the following steps. These steps are highlighted for you
in the code sample below.
- Register the XML parser we wish to use
- Create an XMLReader instance
- Create a class instance responsible for handling SAX events.
- Assign a event handler to our XMLReader instance
- Parse the document passing the XMLReader an XML file. (In this example we will be using "order.xml" a file containing
the XML code shown earlier.)
|
In this example our class will extend DefaultHandler. DefaultHandler is an adapter class that will allow
us to process only the SAX events that we are interested in rather than implementing all possible SAX
events.
package xml;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
public class SimpleSax extends DefaultHandler
{
public static void main(String[] args)
{
try
{
Class c = Class.forName("org.apache.xerces.parsers.SAXParser");
XMLReader reader = (XMLReader)c.newInstance();
SimpleSax ss = new SimpleSax();
reader.setContentHandler(ss);
reader.parse("department.xml");
}
catch(Exception e){System.out.println(e);}
}
public void startElement(String uri, String local_name, String raw_name, Attributes amap)
throws SAXException
{
System.out.println("start " + local_name + " found ");
}
public void endElement(String uri, String local_name, String raw_name)
throws SAXException
{
System.out.println("end " + local_name + " found");
}
public void startDocument() throws SAXException
{
System.out.println("start document");
}
public void endDocument() throws SAXException
{
System.out.println("end document");
}
public void characters(char[] ch, int start, int length)
throws SAXException
{
System.out.println("characters " + new String(ch,start,length) + " found");
}
}
The SAX parser has several different events, four major ones of which we will cover today. The events that we handled are defined as follows:
startElement(...)
This event is fired whenever a starting tag of an element is found i.e. -
endElement(...)
This event is fired whenever an ending tag of an element is found i.e.
characters(...)
This event is fired whenever text data is found inside of an element i.e the "Soccer Ball"
between the and tags.
startDocument(...)
This event is fired when the start of an XML document is found. i.e.
endDocument(...)
This event is fired when the end of an XML document is found.
Upon running the above example you should witness the various SAX events being fired, dumping information out to the
console. Well unfortunately my time is up. Next week we shall get a little more detailed in that we will define a DTD for validation of our
XML document and get a little trickier with our SAX processing. Until then, happy programming.
| Sponsored Links - please visit our sponsors |
| FTP Applet | | Add file transfer capabilities to your web pages. | | http://www.jscape.com/ftpapplet/index.html |
| SSH Factory | | automate telnet and SSH tasks | | http://www.jscape.com/sshfactory/ |
| Learn Java Server Faces! | | Use WebGalileo Faces JSF components to quickly create Java based web apps. | | http://www.jscape.com/webgalileofaces/ |
| Java FTP Component | | Easily add FTP to your Java apps. | | http://www.jscape.com/inetfactory/ftp.html |
| Secure FTP Applet | | Connect to FTP securely from within your browser. | | http://www.jscape.com/sftpapplet/ |
Sponsor this site
| |