Home arrow XML arrow Page 5 - Java and XML Basics, Part 2
XML

Java and XML Basics, Part 2


Last time, we learned about JAXP, Xerces, DOM and the javax.xml.parsers Java Package. How about getting a little taste of the SAX interfaces? We look at available classes and interfaces, and learn how to use SAX for XML Processing. Given SAX's power, perhaps we can look forward to the day when we'll be translating not just XML, but maybe even Klingon! Maybe not. Before you get started, you'll want to download the support files for this tutorial.

Author Info:
By: Liviu Tudor
Rating: 5 stars5 stars5 stars5 stars5 stars / 39
March 10, 2004
TABLE OF CONTENTS:
  1. · Java and XML Basics, Part 2
  2. · DefaultHandler class
  3. · Parser Reports
  4. · Simple State Machine
  5. · Using SAX for XML Processing
  6. · One Last SAX Trick

print this article
SEARCH DEVARTICLES

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?


blog comments powered by Disqus
XML ARTICLES

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials