Real'sHowTo AddThis Feed Button
Custom Search

Parse using SAX or DOMTag(s): XML

DOM (Document Object Model) represents the XML as hierarchy to simplify the access. Since everything is in memory, it is more ressource intensive.

SAX - creates events and calls callback methods that the programmer write to handle them.

Both examples read this XML data file [howto.xml]

<?xml version="1.0"?>
In both cases , the output is
Title: Java
Url: http://www.rgagnon/javahowto.htm
Title: PowerBuilder
Url: http://www.rgagnon/pbhowto.htm
Title: Javascript
Url: http://www.rgagnon/jshowto.htm
Title: VBScript
Url: http://www.rgagnon/vbshowto.htm
// jdk1.4.1
import org.xml.sax.*;
import org.xml.sax.helpers.*;

// using SAX
public class HowToListerSAX {
  class HowToHandler extends DefaultHandler {
    boolean title = false;
    boolean url   = false;
    public void startElement(String nsURI, String strippedName,
                            String tagName, Attributes attributes)
       throws SAXException {
     if (tagName.equalsIgnoreCase("title"))
        title = true;
     if (tagName.equalsIgnoreCase("url"))
        url = true;

    public void characters(char[] ch, int start, int length) {
     if (title) {
       System.out.println("Title: " + new String(ch, start, length));
       title = false;
     else if (url) {
       System.out.println("Url: " + new String(ch, start,length));
       url = false;

    public void list( ) throws Exception {
       XMLReader parser =
       parser.setContentHandler(new HowToHandler( ));

    public static void main(String[] args) throws Exception {
       new HowToListerSAX().list( );


// jdk1.4.1
import javax.xml.parsers.*;
import org.w3c.dom.*;

// using DOM
public class HowtoListerDOM {
 public static void main(String[] args) {
   File file = new File("howto.xml");
   try {
     DocumentBuilder builder =
     Document doc = builder.parse(file);

     NodeList nodes = doc.getElementsByTagName("topic");
     for (int i = 0; i < nodes.getLength(); i++) {
       Element element = (Element) nodes.item(i);

       NodeList title = element.getElementsByTagName("title");
       Element line = (Element) title.item(0);

       System.out.println("Title: " + getCharacterDataFromElement(line));

       NodeList url = element.getElementsByTagName("url");
       line = (Element) url.item(0);
       System.out.println("Url: " + getCharacterDataFromElement(line));

   catch (Exception e) {
 public static String getCharacterDataFromElement(Element e) {
   Node child = e.getFirstChild();
   if (child instanceof CharacterData) {
     CharacterData cd = (CharacterData) child;
       return cd.getData();
   return "?";

blog comments powered by Disqus

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2015
[ home ]