XML
  Home arrow XML arrow Page 6 - 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 
Sun Developer Network 
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 - One Last SAX Trick


    (Page 6 of 6 )

    One last trick when it comes to SAX – the SAX specification reads the following under the characters function:

    The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information.

    This may not raise any issues with you, but since you took the JAXP path, it means that you are possibly thinking of writing portable code – i.e. XML parser independent; as stated in the previous article, each parser has its own particularities, even if they do share the same set of interfaces (JAXP) – even more so if the interface allows it!  So it’s best if you cater for all possible particularities of these beasts.  Now, let’s re-read the above paragraph and see what it actually means:

    Some SAX parsers might call this function only once and include the whole set of literals found inside a tag, while others may call this function as many times they want with chunks of data – even to the point where they could call this function passing just one single character!

    In such cases, our state-machine has to cater for this and paste all these pieces together to form the full string – so really, if we want to be “portable”, we should write our code as it follows (SimpleSAXParser6.java):


    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.
       m_Path 

    "";
      
    }
     
    }
    }
     
    public 
    void endElementString namespaceURIString localNameString qName 
    )
    {
     
    if( qName.equals(APP_TAG) && m_Application 
    )
     
    {
      m_Application 

    false;
      System
    .out.println"(" + (++nApp) + ") " m_Path 
    );
      
    return;
     
    }
     
     if( 
    qName.equals(SESSION_TAG) )
      m_AutomaticSession 

    false;
    }
     
    public 
    void characterschar[] chint startint length )
    {
     
    if( 
    m_Application )
      m_Path 
    += new Stringchstartlength 
    ); //paste this chunk
    }

    In other words, keep building up the String that stores the application path until we are notified that the application element has ended!
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    XML ARTICLES

    - 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
    - UI Design with Java and XML Toolkits
    - Displaying ADO Retrieved Data with XML Islan...
    - Widget Walkthrough
    - Introduction to Widgets
    - The Why and How of XML Data Islands






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