Wednesday, October 09, 2019

Validate xml using xsd and java

1- Lets say we have following XSD files that is going to validate the xml.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:element name="root" type="rootType">
</xs:element>
<xs:complexType name="rootType">
<xs:sequence>
<xs:element name="addresses" type="addressesType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="addressesType">
<xs:sequence>
<xs:element name="address" type="addressType" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="addressType">
<xs:sequence>
<xs:element type="xs:short" name="flatnumber"/>
<xs:element type="xs:string" name="flatname"/>
<xs:element type="xs:string" name="societyname"/>
<xs:element type="xs:string" name="country"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
2- Lets say we have following two xml first is valid and second is invalid
1-
<?xml version="1.0" encoding="UTF-8"?>
<root>
<addresses>
<address>
<flatnumber>101</flatnumber>
<flatname>flatname1</flatname>
<societyname>society name 1</societyname>
<country>India</country>
</address>
<address>
<flatnumber>102</flatnumber>
<flatname>flatname2</flatname>
<societyname>society name 2</societyname>
<country>UK</country>
</address>
</addresses>
</root>
2-
<?xml version="1.0" encoding="UTF-8"?>
<root>
<addresses>
<address>
<flatnumber>test</flatnumber>
<flatname>flatname1</flatname>
<societyname>society name 1</societyname>
<country>India</country>
</address>
<address>
<flatnumber>102</flatnumber>
<flatname>flatname2</flatname>
<societyname>society name 2</societyname>
<country>UK</country>
</address>
</addresses>
</root>
3- Following is the java class that is used to check if the given xml adhere to valid xsd file or not
package com.test.siddhu;
import org.xml.sax.SAXException;
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 java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;
public class XMLValidator {
//For valid
//For non valid
//public static final String XML_FILE = "C:\\Latest_JAVA_eclipse_WorkSpace\\TestXMLFiles\\src\\com\\test\\siddhu\\not_valid_address.xml";
public static final String SCHEMA_FILE = "C:\\Latest_JAVA_eclipse_WorkSpace\\TestXMLFiles\\src\\com\\test\\siddhu\\address.xsd";
public static void main(String[] args) {
XMLValidator XMLValidator = new XMLValidator();S
boolean valid = XMLValidator.validate(XML_FILE, SCHEMA_FILE);
System.out.printf("%s validation = %b.", XML_FILE, valid);
}
private boolean validate(String xmlFile, String schemaFile) {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
Schema schema = schemaFactory.newSchema(new File(schemaFile));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File(xmlFile)));
return true;
} catch (SAXException | IOException e) {
e.printStackTrace();
return false;
}
}
private String getResource(String filename) throws FileNotFoundException {
URL resource = getClass().getClassLoader().getResource(filename);
Objects.requireNonNull(resource);
return resource.getFile();
}
}
For valid xml we will get below output
Image1

For in valid xml we will get below output saying flatnumber should be number/integer and not the text like Test

Image2

No comments: