XML
  Home arrow XML arrow Page 5 - Java and XML Basics, Part 2
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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

Java and XML Basics, Part 2
By: Liviu Tudor
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 38
    2004-03-10

    Table of Contents:
  • Java and XML Basics, Part 2
  • DefaultHandler class
  • Parser Reports
  • Simple State Machine
  • Using SAX for XML Processing
  • One Last SAX Trick

  • 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


    Java and XML Basics, Part 2 - Using SAX for XML Processing


    (Page 5 of 6 )

    Now that we had a look at the way SAX works, let’ see how we can actually make usage of it in our programs. For this, we will revert back to the example we have used in the previous article as well:

    Let’s imagine an application that is only interested in the automatically-started processes that only needs to know the name of the application that was started. In XML terms, this translates to “look for all the session elements with the attribute type set to automatic and only retrieve the text stored within the application tag located right underneath this session element.” (Believe me, when I say “translates to”, I don’t mean it in the way you say "paghmo' tIn mIS" translates to "Much Ado about Nothing" in Klingon!) So let’s change our code so that it implements a state machine which will tell us if we are underneath a session element or not, and whether it is an automatic process or not. Based on this we can then decide whether we are going to look for an application element underneath. Once we establish that we are looking for an application tag, and once it is found we will print out the characters found inside this tag.

    So we will change our code around like this (SimpleSAXParser5.java) – bear in mind that we will only process the start/endElement notifications together with characters:


    public void startElementString namespaceURIString localName
    String qNameAttributes atts )
    {
     
    if( !m_AutomaticSession 
    )
     
    {
      
    /**
       * If not inside a session, keep 
    looking for the "session" tag
       */

      
    if( 
    qName.equals(SESSION_TAG
    )

      
    {
       
    /**
        * If inside a 
    session check the "type" tag
        
    */

       String type 
    atts.getValueTYPE_ATTR 
    );
       
    if( (type != null) && type.equals("automatic"
    )

        m_AutomaticSession 
    true//we are from now 
    on inside an "automatic" 
    session
      
    }
     
    }
     else
     
    {
      
    /**
       
    * Already in a session, keep looking for the "application" tag
       
    */

      
    if( qName.equals(APP_TAG
    )

      
    {
       m_Application 
    true//from now on 
    print the applications we have found.
       System
    .out.print( "(" 
    + (++nApp) + ") " );
      
    }
     
    }
    }
     
    public 
    void endElementString namespaceURIString localNameString qName 
    )
    {
     
    if( qName.equals(APP_TAG
    )

     
    {
      m_Application 

    false;
      
    return;
     
    }
     
     if( 
    qName.equals(SESSION_TAG) )
      m_AutomaticSession 

    false;
    }
     
    public 
    void characterschar[] chint startint length )
    {
     
    if( 
    m_Application )
      System
    .out.println( new String(chstartlength
    );

    }

    Basically, the state machine logic is implemented in the start/endElement methods, while the actual execution happens in the characters function – easy, huh?

    More XML Articles
    More By Liviu Tudor


     

    XML ARTICLES

    - Using Regions with XSL Formatting Objects
    - Using XSL Formatting Objects
    - More Schematron Features
    - Schematron Patterns and Validation
    - Using Schematron
    - Datatypes and More in RELAX NG
    - Providing Options in RELAX NG
    - An Introduction to RELAX NG
    - Path, Predicates, and XQuery
    - Using Predicates with XQuery
    - Navigating Input Documents Using Paths
    - XML Basics
    - Introduction to XPath
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek