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 startElement
( String namespaceURI, String localName,
String qName, Attributes 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.getValue( TYPE_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 endElement( String namespaceURI, String localName, String qName
)
{
if( qName.equals(APP_TAG)
)
{
m_Application =
false;
return;
}
if( qName.equals(SESSION_TAG) )
m_AutomaticSession =
false;
}
public void characters( char[] ch, int start, int length )
{
if(
m_Application )
System.out.println( new String(ch, start, length)
);
}
Basically, the state machine logic is implemented in the start/endElement methods, while the actual execution happens in the characters function – easy, huh?
Next: One Last SAX Trick >>
More XML Articles
More By Liviu Tudor