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


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