advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Schema Validation with JAXP or JDOM :

Validating with the JAXP 1.3 Validation API

The validation API in J2SE 5.0 is included in the javax.xml.validation package. To validate with Validator class, import the javax.xml.validation package:
import javax.xml.validation.*;
To use this class with an XML Schema, you'll need a schema object representation of the schema. Create a schema object from the SchemaFactory class, which is a schema compiler you can obtain by using the static method newInstance(). Here's what the object should look like:
SchemaFactory factory=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema=factory.newSchema(new File("c:/schemas/catalog.xsd"));
The only argument to the newInstance() method is a Schema language constant whose value is XMLConstants.W3C_XML_SCHEMA_NS_URI. This is the same as http://www.w3.org/2001/XMLSchema".Validator class validates an XML document with respect to an XML schema.

To create a Validator object from a Schema object:

Validator validator=schema.newValidator();
To report validation errors, define an ErrorHandler class for the Validator. This ErrorHandler class must extend the DefaultHandler, which implements the ErrorHandler interface:
private class ErrorHandlerImpl extends DefaultHandler	
   {		
       public boolean  validationError = false;	
       public SAXParseException saxParseException=null;
       
       public void error(SAXParseException exception) throws SAXException	
       {
           validationError = true;	
           saxParseException=exception;
       }		
       public void fatalError(SAXParseException exception) throws SAXException		
       {		
            validationError = true;	
            saxParseException=exception;	
       }		
       public void warning(SAXParseException exception) throws SAXException		
       {		
       }	
   }
Set an instance of an ErrorHandler class on the validator with the setErrorHandler() method:
ErrorHandlerImpl errorHandler=new ErrorHandlerImpl();
validator.setErrorHandler(errorHandler);
If a you generate a validation error, it's registered with the ErrorHandler. To validate an XML document, create a StreamSource from the XML document, invoke the validate() method on the validator, and pass it the stream source for the document:
StreamSource streamSource=new StreamSource(xmlDocument);
validator.validate(streamSource);

Listing 4 shows how to validate using the J2SE 5.0 validation API.

Run the XMLSchemaValidator.java application in your command line or your Java IDE. Here's what the output should be:

XML document is Valid
Now, add an error like you did in the JDOM section. This also generates a validation error.

There are benefits and drawbacks to each of these methods. With the JDOM API, validation is an integral part of XML parsing. But the JAXP 1.3 API decouples validation from the XML parsing process, thus increasing the performance of schema validation. The amount of performance gain you'll see is directly proportionate to the ratio size of XML schema/size of XML document. Another advantage to the JAXP 1.3 API is that you can use any schema language, whereas with JDOM, you must use the W3C XML Schema.

Home / Articles / Schema Validation with JAXP or JDOM / 1 / 2 / 3 /

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs