Skip to content

Instantly share code, notes, and snippets.

@ryan-beckett
Created October 4, 2012 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryan-beckett/3836985 to your computer and use it in GitHub Desktop.
Save ryan-beckett/3836985 to your computer and use it in GitHub Desktop.
Validates XML files against a specified schema or the default WC3 standard.
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
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.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class XMLValidator
{
private URL schemaURL;
private Validator validator;
public XMLValidator()
{
this(null);
}
public XMLValidator(String schemaURL)
{
try
{
this.schemaURL = (schemaURL == null) ? null : new URL(schemaURL);
SchemaFactory factory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = (schemaURL == null) ? factory.newSchema() : factory
.newSchema(this.schemaURL);
validator = schema.newValidator();
} catch (Exception e)
{
e.printStackTrace();
}
}
public boolean isValid(URL url) throws IOException, SAXException
{
return isValid(new StreamSource(url.openStream()));
}
public boolean isValid(String file) throws IOException, SAXException
{
return isValid(new StreamSource(new BufferedInputStream(
new FileInputStream(file))));
}
private boolean isValid(Source src) throws IOException
{
ValidatingHandler errorHandler = new ValidatingHandler();
validator.setErrorHandler(errorHandler);
try
{
validator.validate(src);
} catch (SAXException e)
{
return false;
}
return errorHandler.errorOccured();
}
private class ValidatingHandler implements ErrorHandler
{
private boolean parseError;
@Override
public void error(SAXParseException e)
{
parseError = true;
}
@Override
public void fatalError(SAXParseException e)
{
parseError = true;
}
@Override
public void warning(SAXParseException exception)
{
}
public boolean errorOccured()
{
return parseError;
}
}
public static void main(String[] args) throws Exception
{
String url = "";
XMLValidator validator = new XMLValidator();
System.out.println("Is Valid XML = " + validator.isValid(new URL(url)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment