Wednesday, October 09, 2019

1- Lets say we have following XML files along with DTD added in it
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE root [
<!ELEMENT root (element*)>
<!ELEMENT element (name,surname)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT surname (#PCDATA)>
]>
<root>
<element>
<name>Siddhartha</name>
<surname>Dhumale</surname>
</element>
</root>
2- Following is the java class that is used to check if the given xml adhere to valid xsd file or not
package com.siddhu;
import java.io.IOException;
// DOM
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
// SAX
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.XMLReader;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;
public class ValidateXMLUsingDTD {
private ValidateXMLUsingDTD() {}


public static boolean validateWithDTDUsingDOM(String xmlFiles)
throws ParserConfigurationException, IOException
{
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.parse(new InputSource(xmlFiles));
System.out.print("XML files is valid using Dom Parser:"+xmlFiles+"---");
return true;
}
catch (ParserConfigurationException pce) {
throw pce;
}
catch (IOException io) {
throw io;
}
catch (SAXException se){
return false;
}
}

public static boolean validateWithDTDUsingSAX(String xmlFiles)
throws ParserConfigurationException, IOException
{
try {

SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.parse(new InputSource( xmlFiles ));
System.out.print("XML files is valid using SAX Parser:"+xmlFiles+"---");
return true;
}
catch (ParserConfigurationException pce) {
throw pce;
}
catch (IOException io) {
throw io;
}
catch (SAXException se){
return false;
}
}

public static void main (String args[]) throws Exception{

System.out.println(ValidateXMLUsingDTD.validateWithDTDUsingDOM("C:\\Latest_JAVA_eclipse_WorkSpace\\TestXMLFilesUsingDTD\\src\\com\\siddhu\\siddhu.xml"));
System.out.println(ValidateXMLUsingDTD.validateWithDTDUsingSAX("C:\\Latest_JAVA_eclipse_WorkSpace\\TestXMLFilesUsingDTD\\src\\com\\siddhu\\siddhu.xml"));

}
}
For valid xml we will get below output
Image3

For in valid xml we will get below output

Image4


Image5

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