XML
  Home arrow XML arrow Page 4 - 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 - Simple State Machine


    (Page 4 of 6 )

    In the next example (SimpleSAXParser4.java) we will build a simple “state” machine that will only give us the level we are currently at in the DOM tree. This will increase every time startElement is called and decrease every time endElement is called – and based on its value we will indent the lines accordingly:


    ...
    /**
     * Our "state" machine -- the current level we 
    are on
     * (0 means root level)
     */

    private 
    int   m_Level 
    0;
    private 
    void spaceForLevel()
    {
     
    if( m_Level <= 
    )
      
    return;
     
    for( int i 0m_Leveli++ 
    )

      System
    .out.print( "  " );
    }
    ...
    public void 
    startDocument
    ()
    {
     m_Level 

    0;
     spaceForLevel
    ();
     System
    .out.println"Document started." 
    );
    }
    public 
    void 
    endDocument
    ()
    {
     spaceForLevel
    ();
     System
    .out.println
    "Document ended." );
     m_Level 
    0;
    }
    public 
    void startElementString namespaceURIString localNameString 
    qName
    Attributes atts 
    )
    {
     spaceForLevel
    ();
     System
    .out.println"Started element " 
    qName );
     m_Level
    ++;
    }
    public 
    void endElementString namespaceURIString localNameString qName 
    )
    {
     m_Level
    --;
     spaceForLevel
    ();
     System
    .out.println
    "Ended element " qName );
    }
    public 
    void characterschar[] chint startint length 
    )
    {
     spaceForLevel
    ();
     System
    .out.println"Encountered 
    characters:’" 
    + new String(chstartlength) + “’” 
    );
    }
    ...

    Run this code against simple1.xml and it now begins to make sense!

    Document started.
    Started element applog
      Encountered characters:''
      Encountered characters:'
    '
      Encountered characters:'  '
      Started element session
        Encountered characters:''
        Encountered characters:'
    '
        Encountered characters:'    '
        Started element duration
          Encountered characters:'01:00:00'
        Ended element duration
        Encountered characters:''
        Encountered characters:'
    '
        Encountered characters:'    '
        Started element files
          Encountered characters:'7'
        Ended element files
        Encountered characters:''
        Encountered characters:'
    '
        Encountered characters:'    '
        Started element application
          Encountered characters:'notepad.exe'
        Ended element application
        Encountered characters:''
        Encountered characters:'
    '
        Encountered characters:'    '
        Started element comments
          Encountered characters:'Started by the administrator to edit some config f
    iles.'
        Ended element comments
        Encountered characters:''
        Encountered characters:'
    '
        Encountered characters:'  '
      Ended element session
      Encountered characters:''
      Encountered characters:'
    '
      Encountered characters:''
      Encountered characters:'
    '
      Encountered characters:'  '
      Started element session
        Encountered characters:''
        Encountered characters:'
    '
        Encountered characters:'    '
        Started element duration
          Encountered characters:'00:10:00'
        Ended element duration
        Encountered characters:''
        Encountered characters:'
    '
        Encountered characters:'    '
        Started element distance
          Encountered characters:'37'
        Ended element distance
        Encountered characters:''
        Encountered characters:'
    '
        Encountered characters:'    '
        Started element application
          Encountered characters:'grep.exe'
        Ended element application
        Encountered characters:''
        Encountered characters:'
    '
        Encountered characters:'    '
        Started element comments
          Encountered characters:'Probably part of one of the maintenance scripts.'
        Ended element comments
        Encountered characters:''
        Encountered characters:'
    '
        Encountered characters:'  '
      Ended element session
      Encountered characters:''
      Encountered characters:'
    '
    Ended element applog
    Document ended.
    Parsing successfull!

    NOTE  We’ve added the apostrophes so we can see clearly whether there is actually any character or whether it is just a dummy call we are receiving

    It is clear now that the parser (1) starts with the applog tag (which we were expecting, as this is the root element), then (2) comes over the spaces in between the end of the applog tag and the beginning of the session tag. (3) Then, the parsers reaches the session tag and sends a notification. (4) All the spaces then up until the beginning of the duration tag, when it (5) sends us a notification again. (6) The parser then finds the characters enclosed within the duration tag, so it (7) notifies our class again and so on. Now, based on this code and the little state machine, you can probably figure out how a DocumentBuilder class (the one we have used in the previous article) builds up the whole DOM little by little...

    More XML Articles
    More By Liviu Tudor


     

    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 2 hosted by Hostway
    Stay green...Green IT