Login
Register

Home

Trainings

Fusion Blog

EBS Blog

Authors

CONTACT US

Fusion Blog
  • Register

Oracle Gold Partners, our very popular training packages, training schedule is listed here
Designed by Five Star Rated Oracle Press Authors & Oracle ACE's.

webinar new

Search Courses

 

Objective:
In the previous article Method Overriding in Java, Method overriding is explained in detail with example. In this article, we will learn how to validate XML against XSD in java.

How to validate XML against XSD in java:
Java XML Validation API can be used to validate XML against an XSD. javax.xml.validation.Validator class is used in this program to validate xml file against xsd file.Here are the sample XSD and XML files used.

Employee.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.journaldev.com/Employee"
xmlns:empns="http://www.journaldev.com/Employee" elementFormDefault="qualified">
<element name="empRequest" type="empns:empRequest"></element>
<element name="empResponse" type="empns:empResponse"></element>
<complexType name="empRequest">
<sequence>
<element name="id" type="int"></element>
</sequence>
</complexType>
<complexType name="empResponse">
<sequence>
<element name="id" type="int"></element>
<element name="role" type="string"></elemen>
<element name="fullName" type="string"></element>
</sequence>
</complexType>
</schema>

Notice that above XSD contains two root element and namespace also, I have created two sample XML file from XSD using Eclipse.


EmployeeRequest.xml

<?xml version="1.0" encoding="UTF-8"?>

<empns:empRequest xmlns:empns="http://www.journaldev.com/Employee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.journaldev.com/Employee Employee.xsd ">

EmployeeResponse.xml

<?xml version="1.0" encoding="UTF-8"?>

<empns:empResponse xmlns:empns="http://www.journaldev.com/Employee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.journaldev.com/Employee Employee.xsd ">
<empns:id>1</empns:id>
<empns:role>Developer</empns:role>
<empns:fullName>Pankaj Kumar</empns:fullName>
</empns:empResponse>

Here is another XML file that doesn’t confirms to the Employee.xsd.


employee.xml

<?xml version="1.0"?>
<Employee>
<name>Pankaj</name>
<age>29</age>
<role>Java Developer</role>
<gender>Male</gender>
</Employee>

Here is the program that is used to validate all three XML files against the XSD. The validateXMLSchemamethod takes XSD and XML file as argument and return true if validation is successful or else returns false.

XMLValidation.java

package com.journaldev.xml;
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;
public class XMLValidation {
public static void main(String[] args) {

     System.out.println("EmployeeRequest.xml validates against Employee.xsd? "+validateXMLSchema("Employee.xsd", "EmployeeRequest.xml"));

     System.out.println("EmployeeResponse.xml validates against Employee.xsd? "+validateXMLSchema("Employee.xsd", "EmployeeResponse.xml"));

     System.out.println("employee.xml validates against Employee.xsd? "+validateXMLSchema("Employee.xsd", "employee.xml"));

     }

   public static boolean validateXMLSchema(String xsdPath, String xmlPath){

       try {

           SchemaFactory factory =

                   SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

           Schema schema = factory.newSchema(new File(xsdPath));

           Validator validator = schema.newValidator();

           validator.validate(new StreamSource(new File(xmlPath)));

       } catch (IOException | SAXException e) {

           System.out.println("Exception: "+e.getMessage());

           return false;

       }

       return true;

   }

}


Output of the above program is:

EmployeeRequest.xml validates against Employee.xsd? true

EmployeeResponse.xml validates against Employee.xsd? true

Exception: cvc-elt.1: Cannot find the declaration of element 'Employee'.

employee.xml validates against Employee.xsd? false

Benefit of using java XML validation API is that we don’t need to parse the file and there are no third party APIs used.






Varun Kapila

Add comment


Security code
Refresh

About the Author

Varun Kapila

Search Trainings

Fully verifiable testimonials

Apps2Fusion - Event List

<<  Apr 2024  >>
 Mon  Tue  Wed  Thu  Fri  Sat  Sun 
  1  2  3  4  5  6  7
  8  91011121314
15161718192021
22232425262728
2930     

Enquire For Training

Fusion Training Packages

Get Email Updates


Powered by Google FeedBurner