XML
  Home arrow XML arrow Page 3 - 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 - Parser Reports


    (Page 3 of 6 )

    We will get back to “listening” to document errors in a minute; now let’s take a look at what our parser reports in terms of document content progress. We will override the following functions declared in the ContentHandler interface:

    • startDocument
    • endDocument
    • startElement
    • endElement
    • characters

    All we do in our code at this stage (SampleSAXParser3.java) is print out messages regarding the notification(s) we have received:


    ...
    public void 
    startDocument
    ()
    {
     System
    .out.println"Document started." 
    );
    }
    public 
    void endDocument()
    {
     System
    .out.println"Document ended." 
    );
    }
    public 
    void startElementString namespaceURIString localNameString 
    qName
    Attributes atts )
    {
     System
    .out.println"Started element " 
    qName );
    }
    public 
    void endElementString namespaceURIString localNameString qName 
    )
    {
     System
    .out.println"Ended element " qName );
    }
    public 
    void characterschar[] chint startint length 
    )
    {
     System
    .out.println"Encountered characters:" + new String(ch
    startlength) );
    }

    Running this class against our simple1.xml document will produce something like this:


    java -classpath "%CLASSPATH%;." SimpleSAXParser3 
    simple1
    .xml
    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 files.
    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
    !

    As you can see, our parser is doing its job properly!  Now, assuming we want to make a pretty-printing of this output and want to indent each line depending on the “depth” of the element in the DOM tree. "Easy," some might say--but then again, some said 640k RAM would be enough for any possible programmer out there! The problem is that from this code we simply can’t tell how deep we are in the tree when we receive one of the contents notifications!  In the case of the DOM interface by using getParent--or getChild--we could figure out exactly where we were in the DOM; however, SAX doesn’t do that for us. Instead, it is up to each program using SAX to build its own state machine based on the notifications received.

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek