Using Java API for XML validation of a document

An XML Schema defines how an instance of an XML document should look like. Most of the XML documents that are created are usually validated against a particular schema so that the XML document conforms to the standards needed before it is processed.

_______________________________________________


_______________________________________________

This ensures that the XML document does not contain any irrelevant element that might hinder the processing of the XML document.

The package javax.xml.validation contains all the classes needed for validating an XML document against a schema. The other imports that might be needed to validate an XML document are given below.

import java.io.*;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

Now the first step is to create a new document object and load the XML file into it. For this you will be using DocumentBuilderFactory and the DocumentBuilder objects. Create an instance of a SchemaFactory object.

Then create a StreamSource for loading the Schema file into it. Now the next step is to create a Schema object and use the SchemaFactory's newSchema() method to load the schema into it.

Now for doing the validation you have to create a Validator object and link that to the Schema. Now use the validate() method of the validator object and load the XML source for validation against the particular schema.

Working examples for using the above methods are available in plenty in the internet.

Havelsan Takes a Leading Role in Technology and Industry
Havelsan is one of the leading companies providing creative and innovative solutions in the field of defence and IT in and around Turkey. The company is involved in significant projects such as spe...

WSO2 Launches Newest Versions of WSO2 Business Process Server, WSO2 Data Services Server, WSO2 BAM, WSO2 Gadget Server ...
MOUNTAIN VIEW, Calif.----WSO2 today announced the availability of WSO2 Business Process Server 2.0, WSO2 Data Services Server 2.5, WSO2 Business Activity Monitor 1.1, WSO2 Gadget Server 1.1, and WSO2 Mashup Server 2.1.

MyEclipse 8.6 Makes JavaScript Developers Smarter and Application Management Simpler
Production release gives users unmatched flexibility and speed in creating and deploying applications



FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

_______________________________________

Recommended XML Books

cover
cover
cover
cover
cover
cover






 

 

FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.

Name:

Email:

Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.

 

 

Add to My Yahoo!


New XML Books Titles - Best Seller's @ Amazon.com

Visit XML Training Material Guide Homepage

Technology helps engage students - how do classroom instruction and social dynamics change when the classroom is completely dependant on modern technology?