How to validate a XML file using XSD schema file simple example (JAVA)
Here is a sample XML file;
<?xml version=”1.0″ encoding=”UTF-8″?>
<?xml-stylesheet type=”text/xsl” href=”bank.xsl”?>
<bnk:Bank
xmlns:bnk=”http://www.tunatore.wordpress.com/bank“
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.tunatore.wordpress.com/bank bank.xsd”>
<bnk:BankName>IT Bank of Turkey</bnk:BankName>
<bnk:Branch>
<bnk:branchID>11111</bnk:branchID>
<bnk:branchName>Istanbul Branch</bnk:branchName>
<bnk:branchLocation>Istanbul</bnk:branchLocation>
</bnk:Branch>
<bnk:Branch>
<bnk:branchID>12345</bnk:branchID>
<bnk:branchName>New York Branch</bnk:branchName>
<bnk:branchLocation>New York</bnk:branchLocation>
</bnk:Branch>
<bnk:Branch>
<bnk:branchID>222222</bnk:branchID>
<bnk:branchName>Miami Branch</bnk:branchName>
<bnk:branchLocation>Miami</bnk:branchLocation>
</bnk:Branch>
<bnk:Employee>
<bnk:employeeID>12345678</bnk:employeeID>
<bnk:name>Tuna</bnk:name>
<bnk:surname>TORE</bnk:surname>
<bnk:profession>IOC Advocate, Abstract Developer</bnk:profession>
</bnk:Employee>
<bnk:Employee>
<bnk:employeeID>987654321</bnk:employeeID>
<bnk:name>James</bnk:name>
<bnk:surname>Gosling</bnk:surname>
<bnk:profession>Java GURU</bnk:profession>
</bnk:Employee>
<bnk:Employee>
<bnk:employeeID>11111111</bnk:employeeID>
<bnk:name>Linus</bnk:name>
<bnk:surname>Torvalds</bnk:surname>
<bnk:profession>Linux GURU</bnk:profession>
</bnk:Employee>
</bnk:Bank>
XSD file (bank.xsd) for validating the XML (bank.xml)
<?xml version=”1.0″ encoding=”UTF-8″?>
<schema targetNamespace=”http://www.tunatore.wordpress.com/bank“
elementFormDefault=”qualified”
xmlns=”http://www.w3.org/2001/XMLSchema“
xmlns:bnk=”http://www.tunatore.wordpress.com/bank“>
<element name=”Bank” type=”bnk:BankType”></element>
<complexType name=”EmployeeType”>
<sequence>
<element name=”employeeID” type=”string”></element>
<element name=”name” type=”string”></element>
<element name=”surname” type=”string”></element>
<element name=”profession” type=”string”></element>
</sequence>
</complexType>
<complexType name=”BranchType”>
<sequence>
<element name=”branchID” type=”string”></element>
<element name=”branchName” type=”string”></element>
<element name=”branchLocation” type=”string”></element>
<element name=”employee” type=”bnk:EmployeeType” minOccurs=”0″ maxOccurs=”unbounded”></element>
</sequence>
</complexType>
<complexType name=”BankType”>
<sequence>
<element name=”BankName” type=”string” minOccurs=”0″ maxOccurs=”1″></element>
<element name=”Branch” type=”bnk:BranchType” minOccurs=”0″ maxOccurs=”unbounded”></element>
<element name=”Employee” type=”bnk:EmployeeType” minOccurs=”0″ maxOccurs=”unbounded”></element>
</sequence>
</complexType>
</schema>
validate method for validating the XML file defined
public static boolean validate() {
boolean isValid = false;
try {
//location of the XML file
File xmlFile = new File(Transformation.class.getResource(“bank.xml”)
.getFile().replace(“%20″, ” “));
//location of the XSD file
File xsdFile = new File(Transformation.class.getResource(“bank.xsd”)
.getFile().replace(“%20″, ” “));
//Create source objects for XML and XSD files
Source xmlSource = new StreamSource(xmlFile);
Source xsdSource = new StreamSource(xsdFile);
//Create SchemaFactory using XSD file
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(xsdSource);
//Validator will use xsd file for validating the XML file
Validator validator = schema.newValidator();
// Validate the DOM tree.
validator.validate(xmlSource);
isValid = true;
} catch (Exception e) {
e.printStackTrace(System.out);
}
if(isValid == true)
System.out.println(“Document is valid!”);
else
System.out.println(“Document is NOT valid!”);
return isValid;
}
SAMPLE OUTPUT FOR A VALID XML DOCUMENT;
run:
Document is valid!
BUILD SUCCESSFUL (total time: 9 seconds)
SAMPLE OUTPUT FOR AN INVALID XML DOCUMENT;
Use the following invalid XML document
<?xml version=”1.0″ encoding=”UTF-8″?>
<?xml-stylesheet type=”text/xsl” href=”bank.xsl”?>
<bnk:Bank
xmlns:bnk=”http://www.tunatore.wordpress.com/bank“
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.tunatore.wordpress.com/bank bank.xsd”>
<bnk:NOTVALIDTAG>THIS ELEMENT IS NOT VALID</bnk:NOTVALIDTAG>
<bnk:BankName>IT Bank of Turkey</bnk:BankName>
<bnk:Branch>
<bnk:branchID>11111</bnk:branchID>
<bnk:branchName>Istanbul Branch</bnk:branchName>
<bnk:branchLocation>Istanbul</bnk:branchLocation>
</bnk:Branch>
<bnk:Branch>
<bnk:branchID>12345</bnk:branchID>
<bnk:branchName>New York Branch</bnk:branchName>
<bnk:branchLocation>New York</bnk:branchLocation>
</bnk:Branch>
<bnk:Branch>
<bnk:branchID>222222</bnk:branchID>
<bnk:branchName>Miami Branch</bnk:branchName>
<bnk:branchLocation>Miami</bnk:branchLocation>
</bnk:Branch>
<bnk:Employee>
<bnk:employeeID>12345678</bnk:employeeID>
<bnk:name>Tuna</bnk:name>
<bnk:surname>TORE</bnk:surname>
<bnk:profession>IOC Advocate, Abstract Developer</bnk:profession>
</bnk:Employee>
<bnk:Employee>
<bnk:employeeID>987654321</bnk:employeeID>
<bnk:name>James</bnk:name>
<bnk:surname>Gosling</bnk:surname>
<bnk:profession>Java GURU</bnk:profession>
</bnk:Employee>
<bnk:Employee>
<bnk:employeeID>11111111</bnk:employeeID>
<bnk:name>Linus</bnk:name>
<bnk:surname>Torvalds</bnk:surname>
<bnk:profession>Linux GURU</bnk:profession>
</bnk:Employee>
</bnk:Bank>
OUTPUT OF THE PROGRAM
run:
org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element ‘bnk:NOTVALIDTAG’. One of ‘{“http://www.tunatore.wordpress.com/bank”:BankName, “http://www.tunatore.wordpress.com/bank”:Branch, “http://www.tunatore.wordpress.com/bank”:Employee}’ is expected.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:417)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3182)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1806)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:705)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2755)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.jaxp.validation.StreamValidatorHelper.validate(StreamValidatorHelper.java:144)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:111)
at javax.xml.validation.Validator.validate(Validator.java:127)
at xmlxsltexample.Transformation.validate(Transformation.java:75)
at xmlxsltexample.Transformation.produceHTMLfromXMLwithXSL(Transformation.java:30)
at xmlxsltexample.Transformation.main(Transformation.java:22)
Document is NOT valid!
BUILD SUCCESSFUL (total time: 2 seconds)