XML
  Home arrow XML arrow Page 5 - XML Parsing With SAX and Xerces (part ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

XML Parsing With SAX and Xerces (part 1)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 13
    2002-01-28

    Table of Contents:
  • XML Parsing With SAX and Xerces (part 1)
  • Playing The SAX
  • Reaching For The Nailgun
  • Under The Microscope
  • Sweeping Up The Mess
  • Diving Deeper

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    XML Parsing With SAX and Xerces (part 1) - Sweeping Up The Mess


    (Page 5 of 6 )

    The example you just saw contains a whole bunch of callback method definitions that don't actually do anything. This merely clutters your code while adding very little by way of functionality. Which is why the guys behind Xerces came up with an alternative to the ContentHandler interface, called the DefaultHandler interface.

    This "helper" class implements the ContentHandler class, along with all of its callbacks; however, these callbacks do nothing by default. The developer can then selectively override these empty callbacks as per the requirements of the application - this is more efficient in general, and also results in cleaner code.

    Take a look at this version of the previous example, this one built on the DefaultHandler class instead of the ContentHandler class:

    import org.apache.xerces.parsers.SAXParser; import org.xml.sax.*; import org.xml.sax.helpers.DefaultHandler; import java.io.*; public class MySecondSaxApp extends DefaultHandler { // constructor public MySecondSaxApp (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 when start elements are found public void startElement (String uri, String local, String qName, Attributes atts) { System.out.println ("Found element: " + local); } // everything starts here public static void main (String[] args) { MySecondSaxApp mySecondExample = new MySecondSaxApp(args[0]); } } This example is much more readable than the previous one, primarily because the entire set of callbacks defined previously are conspicuously absent here. As stated above, these empty callbacks are already provided by the DefaultHandler class; a developer only needs to redefine those which are actually required by the application. Note, though, that extending the DefaultHandler class requires you to add one more item to your list of includes at the top of the program: [code] import org.xml.sax.helpers.DefaultHandler;
    Here's the output:

    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

    More XML Articles
    More By icarus, (c) Melonfire


     

       

    XML ARTICLES

    - How to Set Up Podcasting and Vodcasting
    - Creating an RSS Reader Application
    - Building an RSS File
    - An Introduction to XUL Part 6
    - An Introduction to XUL Part 5
    - An Introduction to XUL Part 4
    - An Introduction to XUL Part 3
    - An Introduction to XUL Part 2
    - An Introduction to XUL Part 1
    - XML Matters: Practical XML Data Design and M...
    - Practical XML Data Design and Manipulation f...
    - SimpleXML
    - XForms Basics, Part 3
    - XForms Basics, Part 2
    - XForms Basics





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway