Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
XML Parsing With SAX and Xerces (part 1)
Find out how to parse XML in Java.

| Reaching For The Nailgun |

Let's begin with a simple XML file, which displays the marked-up inventory statement for a business selling equipment for Quake enthusiasts:


<?xml version="1.0"?>
<inventory>
<item>
<id>758</id>
<name>Rusty, jagged nails for nailgun</name>
<supplier>NailBarn, Inc.</supplier>
<cost>2.99</cost>
<quantity>10000</quantity>
</item>
<item>
<id>6273</id>
<name>Power pack for death ray</name>
<supplier>QuakePower.domain.com</supplier>
<cost>9.99</cost>
<quantity>10</quantity>
</item>
</inventory>


Now, we need a simple Java application that will initialize the SAX parser, read and parse the XML file, and fire the callback functions as it encounters tags.


import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.*;
import java.io.*;

// the ContentHandler interface handles all the callbacks
public class MyFirstSaxApp implements ContentHandler {

// constructor
public MyFirstSaxApp (String xmlFile) {

//  create a Xerces SAX parser
    SAXParser parser = new SAXParser();

    //  set the content handler
    parser.setContentHandler(this);

    //  parse the document
    try {
        parser.parse(xmlFile);
    } catch (SAXException e) {
        System.err.println (e);
    } catch (IOException e) {
        System.err.println (e);
    }
}

//  call this when a start tag is found
public void startElement (String uri, String local, String qName, Attributes atts)  {
    System.out.println ("Found element: " + local);
}

// the remaining callback handlers
// they don't do anything right now...but keep reading!
public void setDocumentLocator(Locator locator) {}
public void startDocument() {}
public void endDocument() {}
public void characters(char[] text, int start, int length){}
public void startPrefixMapping(String prefix, String uri) {}
public void endPrefixMapping(String prefix) {}
public void endElement(String namespaceURI, String localName, String qualifiedName) {}
public void ignorableWhitespace(char[] text, int start, int length) throws SAXException {}
public void processingInstruction(String target, String data){}
public void skippedEntity(String name) {}

// everything starts here
public static void main (String[] args) {
    MyFirstSaxApp myFirstExample = new MyFirstSaxApp(args[0]);
}
}


Sure, it looks a little intimidating - but fear not, all will be explained shortly. Before I get to that, though, it's instructive to see what the output of this looks like. So, how about we compile it and run it?


$ javac MyFirstSaxApp.java


Assuming that all goes well, you should now have a class file named "MyFirstSaxApp.class". Copy this class file to your Java CLASSPATH, and then execute it, with the name of the XML file as argument.


$ java MyFirstSaxApp /home/me/sax/inventory.xml


And here's what you should see:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
Found element: inventory
Found element: item
Found element: id
Found element: name
Found element: supplier
Found element: cost
Found element: quantity
Found element: item
Found element: id
Found element: name
Found element: supplier
Found element: cost
Found element: quantity
').'
'

What's happening here? Every time the parser encounters a start tag within the XML document, it calls the startElement()function, which prints the name of the tag to the standard output device. The parser then moves on to the next construct within the document, calling the appropriate callback function to handle it. This process continues until the entire XML document has been processed.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback