Computing Magazine

Validate Xml Against XSD from ClassPath

Posted on the 19 June 2014 by Abhishek Somani @somaniabhi
I have described how to validate xml against schema in another post. Now if you have schema files in your classpath , and if one schema is dependent on other , then you have to do following things to validate it. Create a custom ResourceResolver like this .The prefix is for those schema files which are not in root of classpath . suppose your schema files contained in a folder named schemas in classpath , then provide prefix as /schema.

package com.acs.plum.web.service.util.xml.validator;
import java.io.InputStream;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;
public class ResourceResolver implements LSResourceResolver {
private String prefix ;
public LSInput resolveResource(String type, String namespaceURI,
String publicId, String systemId, String baseURI) {
systemId = prefix +"/" + systemId;
InputStream resourceAsStream = this.getClass().getResourceAsStream(systemId);
return new CustomLSInput(publicId, systemId, resourceAsStream);
}
/**
* @return the prefix
*/
public String getPrefix() {
return prefix;
}
/**
* @param prefix the prefix to set
*/
public void setPrefix(String prefix) {
this.prefix = prefix;
}
}
Create CustomLSInput class .

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import org.w3c.dom.ls.LSInput;
public class MyCustomLSInput implements LSInput {
private String publicId;
private String systemId;
public String getPublicId() {
return publicId;
}
public void setPublicId(String publicId) {
this.publicId = publicId;
}
public String getBaseURI() {
return null;
}
public InputStream getByteStream() {
return null;
}
public boolean getCertifiedText() {
return false;
}
public Reader getCharacterStream() {
return null;
}
public String getEncoding() {
return null;
}
public String getStringData() {
synchronized (inputStream) {
try {
byte[] input = new byte[inputStream.available()];
inputStream.read(input);
String contents = new String(input);
return contents;
} catch (IOException e) {
e.printStackTrace();
}
return null ;
}
}
public void setBaseURI(String baseURI) {
}
public void setByteStream(InputStream byteStream) {
}
public void setCertifiedText(boolean certifiedText) {
}
public void setCharacterStream(Reader characterStream) {
}
public void setEncoding(String encoding) {
}
public void setStringData(String stringData) {
}
public String getSystemId() {
return systemId;
}
public void setSystemId(String systemId) {
this.systemId = systemId;
}
public BufferedInputStream getInputStream() {
return inputStream;
}
public void setInputStream(BufferedInputStream inputStream) {
this.inputStream = inputStream;
}
private BufferedInputStream inputStream;
public MyCustomLSInput(String publicId, String sysId, InputStream input) {
this.publicId = publicId;
this.systemId = sysId;
this.inputStream = new BufferedInputStream(input);

}
}
Now in your main class set resource resolver with proper prefix.

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 XmlValidator
{
public static void main(String[] args)
{
try {
String schemaLang = "http://www.w3.org/2001/XMLSchema";
SchemaFactory factory = SchemaFactory.newInstance(schemaLang);


ResourceResolver resolver = new ResourceResolver();
resolver.setPrefix("/javaroots");//set prefix if your schema is not in the root of classpath
factory.setResourceResolver(new ResourceResolver());

Schema schema = factory.newSchema(new StreamSource(XmlValidator.class.getResourceAsStream("/MarketLive.xsd")));
Validator validator = schema.newValidator();
validator.validate(new StreamSource("D:\\sportschalet\\SchemaNSamples-5.9.8\\test\\sample_300189_3038341.xml"));

System.out.println("Successfully validated");
} catch (SAXException e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}


}
}
Thats it !! now you do not need to copy schemas in a separate folder . You can have them in your classpath .
Post comments and Suggestions !!!
Validate Xml Against XSD from ClassPath
StackOverFlow Ref


Back to Featured Articles on Logo Paperblog

Magazines