Home arrow XML arrow Page 7 - Java and XML Basics, Part 3
XML

Java and XML Basics, Part 3


So far, during this series of articles (part 1, part 2) we've looked at DOM and SAX, and I suppose most of you are thinking which one of the two approaches is preferable? Well, there is no general rule of thumb, but this article might help you make the right decision when you’ll have to.

Author Info:
By: Liviu Tudor
Rating: 5 stars5 stars5 stars5 stars5 stars / 25
April 20, 2004
TABLE OF CONTENTS:
  1. · Java and XML Basics, Part 3
  2. · Which One is the Better One to Use?
  3. · Running the Parser
  4. · Problems with Big XML Files
  5. · Validating Parsers - DOM
  6. · Where do We Get a Validating Parser?
  7. · ErrorHandler
  8. · Validating Parsers - SAX
  9. · Conclusion

print this article
SEARCH DEVARTICLES

Java and XML Basics, Part 3 - ErrorHandler
(Page 7 of 9 )

Now let's return to the error message we have just seen - it mentions something about an ErrorHandler and a setErrorHandler function. Looking through the DocumentBuilder class documentation, we find indeed such a function and the docco says:

public abstract void setErrorHandler(ErrorHandler eh)
Specify the ErrorHandler to be used to report errors present in the XML document to be parsed. Setting this to null will result in the underlying implementation using it's own default implementation and behavior.
Parameters:
eh - The ErrorHandler to be used to report errors present in the XML document to be parsed
.

Now, at this point you have probably spotted the analogy with the SAX parsing, and you have probably remembered the error and fatalError functions we have written in the SimpleSAXParser classes. It is the same ErrorHandler interface that the DefaultHandler class implements in SAX and it is the actual class (interface) that is to receive notifications from the parser in case of warnings, errors and fatal errors.

Let's modify now our code (ValidatingDOMParser.java) and add an ErrorHandler and assign it to the parser:


/**
 * Next, using the factory we create a DOM parser
 */

try
{
 builder 
factoryBuilder.newDocumentBuilder();
}
catch
ParserConfigurationException pce )
{
 System
.err.println"Error creating DOM parser:" );
 pce
.printStackTrace();
 System
.exit( );
}
/**
 * Set the error handler
 */
builder.setErrorHandler( new DOMErrorHandler() );
 
... ... ...
 
/**
 * XML parsing/validating error handler
 */

static 
class DOMErrorHandler implements ErrorHandler
{
 
/**
  * Error -- more than likely a validation error;
  * which means our XML is invalid and we should stop execution
  */

 
public void errorSAXParseException exception )
 
{
  System
.out.println"Validation error: " exception.getMessage() );
  System
.out.println"Execution will stop." );
  System
.exit( );
 
}
  
/**
  * Fatal error -- execution should stop!
  */

 
public void fatalErrorSAXParseException exception )
 
{
  System
.out.println"FATAL error: " exception.getMessage() );
  System
.out.println"Execution will stop." );
  System
.exit( );
 
}
  
/**
  * Warning, execution can continue, the user could be warned though
  */

 
public void warningSAXParseException exception )
 
{
  System
.out.println"Warning: " exception.getMessage() );
 
}
}

If we run this code, we will get the following:

java -classpath "%CLASSPATH%;." ValidatingDOMParser2 employee.xml
Validation error: Attribute value for "name" is #REQUIRED.
Execution will stop.


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