Artículo
Fecha: 2010-03-10 15:22:13
Validate XML against Schema
Method which validates XML against the Schema:
/**
* Validates XML with a XSD
* @param schemaLocation
* @param xml
*/
public void validateXML(URL schemaLocation, Source xml) {
try
{
// Lookup a factory for the W3C XML Schema language
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
// Compile the schema.
Schema schema = factory.newSchema(schemaLocation);
// Get a validator from the schema.
Validator validator = schema.newValidator();
// Parse the document you want to check.
//Source source = new StreamSource(xml);
// Check the document
validator.validate(xml);
logger.info("xml is valid");
}
catch (SAXException ex) {
logger.error("method validateXML: " + xml + " is not valid because " + ex.getMessage());
}
catch (IOException ex) {
logger.error("method validateXML: " + xml + " is not valid because " + ex.getMessage());
}
}
_______________________________________________________________________
To call the method:
// validates xml with xsd
Source xml = new StreamSource(new File("example.xml"));
URL xsdUrl = this.getClass().getResource("exampleShema.xsd");
this.validateXML(xsdUrl, xml);
|
Autor:Jose Miguel Bataller
Buscador
Últimas entradas
Preseleccionar un option de un element select html con jQuery »
Testing for Empty elements in XSL »
Java Keytool Commands for Managing security certificates »
Execute servlet filter before calling Axis web service »
Set trustStore for SSL connection in Java »
Example of a client-wsdd.config »
Convert Axis Java Object to XML »