Thursday, March 19, 2009

Validate XML file using XSD in Java

XSD stands for XML Schema Definition.
XSD can be used to express a set of rules to which an XML document must conform in order to be considered 'valid' according to that schema. It used to validate the xml file.

Here we have :
1.one xml file (holiday-request.xml),
2. one xsd file (hr.xsd) and
3. one jave file (ValidateXMLUsingXSD.java) to validate xml file using the xsd file.
4. You have to add two jar file in your project (jaxb1-impl.jar and dom4j-1.6.1.jar) that you can google and download.

holiday-request.xml
<?xml version="1.0" encoding="UTF-8"?>
<HolidayRequest xmlns="http://mycompany.com/hr/schemas;
<Holiday>
<StartDate>2006-07-03</StartDate>
<EndDate>2006-07-07</EndDate>
</Holiday>

<Employee>
<Number>42</Number>
<FirstName>Binod</FirstName>
<LastName>Suman</LastName>
</Employee>
</HolidayRequest>

hr.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://mycompany.com/hr/schemas"
xmlns:schemas="http://mycompany.com/hr/schemas;
<xs:element name="HolidayRequest">
<xs:complexType>
<xs:sequence>
<xs:element ref="schemas:Holiday"/>
<xs:element ref="schemas:Employee"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="Holiday">
<xs:complexType>
<xs:sequence>
<xs:element ref="schemas:StartDate"/>
<xs:element ref="schemas:EndDate"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="StartDate" type="xs:NMTOKEN"/>
<xs:element name="EndDate" type="xs:NMTOKEN"/>
<xs:element name="Employee"> <xs:complexType>
<xs:sequence>
<xs:element ref="schemas:Number"/>
<xs:element ref="schemas:FirstName"/>
<xs:element ref="schemas:LastName"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Number" type="xs:integer"/>
<xs:element name="FirstName" type="xs:NCName"/>
<xs:element name="LastName" type="xs:NCName"/>
</xs:schema>

ValidateXMLUsingXSD.java

import java.io.File;
import org.iso_relax.verifier.Schema;
import org.iso_relax.verifier.Verifier;
import org.iso_relax.verifier.VerifierFactory;
import org.iso_relax.verifier.VerifierHandler;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXWriter;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;
public class ValidateXMLUsingXSD {
private String schemaURI;
private Document document;
public ValidateXMLUsingXSD(Document document, String schemaURI) {
this.schemaURI = schemaURI;
this.document = document;
}
public boolean validate() throws Exception {
VerifierFactory factory = new com.sun.msv.verifier.jarv.TheFactoryImpl();
Schema schema = factory.compileSchema(schemaURI);
Verifier verifier = schema.newVerifier();
verifier.setErrorHandler(new ErrorHandler() {
public void error(SAXParseException saxParseEx) {
System.out.println("ERROR :: ");
saxParseEx.printStackTrace();
}
public void fatalError(SAXParseException saxParseEx) {
System.out.println("FATAL ERROR :: ");
saxParseEx.printStackTrace();
}
public void warning(SAXParseException saxParseEx) {
System.out.println("WARNING :: ");
saxParseEx.printStackTrace();
}
});
VerifierHandler handler = verifier.getVerifierHandler();
SAXWriter writer = new SAXWriter(handler);
writer.write(document);
return handler.isValid();
}
public static void main(String []args) throws DocumentException{
SAXReader reader=new SAXReader();
Document document=reader.read(new File("src\\holiday-request.xml"));
ValidateXMLUsingXSD val=new ValidateXMLUsingXSD(document,"src\\hr.xsd");
try {
System.out.println("Result :: "+val.validate());
if(val.validate()){
System.out.println("XML IS COMPATIBLE WITH XSD SCHEMA");
}
} catch (Exception e) {
System.out.println("0");
e.printStackTrace();
}
}
}

Now, if you will run, then you will get "XML IS COMPATIBLE WITH XSD SCHEMA" output, if your xml is compatible with xsd otherwise you will get error output.
For test you can change any datatype in xml file and check the result.
So, simple ............. :)

java

1 comment:

  1. Thanks a ton! the logic in the program was great and have bookmarked!
    Sample Holiday Request Letter

    ReplyDelete

You can put your comments here (Either feedback or your Question related to blog)