use of org.dom4j.io.SAXValidator in project wechat by dllwh.
the class XmlUtils method validateBySchema.
/**
* 检验指定的XML是否符合
*
* @param doc
* 被检验的XML文档
* @param schemePath
* schema的路径
* @return 如果出错,返回错误信息,否则返回null
* @throws ParserConfigurationException
* @throws SAXException
* @throws DocumentException
*/
public static String validateBySchema(Document doc, String schemePath) throws ParserConfigurationException, SAXException, DocumentException {
XMLErrorHandler errorHandler = new XMLErrorHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", "file:" + schemePath);
SAXValidator validator = new SAXValidator(parser.getXMLReader());
validator.setErrorHandler(errorHandler);
if (errorHandler.getErrors().hasContent()) {
return errorHandler.getErrors().asXML();
}
return null;
}
use of org.dom4j.io.SAXValidator in project openolat by klemens.
the class ItemFileResourceValidator method validateDocument.
private boolean validateDocument(Document in) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
SimpleErrorHandler errorHandler = new SimpleErrorHandler();
ItemContentHandler contentHandler = new ItemContentHandler();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setEntityResolver(new IMSEntityResolver());
reader.setErrorHandler(errorHandler);
reader.setContentHandler(contentHandler);
SAXValidator validator = new SAXValidator(reader);
validator.validate(in);
return errorHandler.isValid() && contentHandler.isItem();
} catch (ParserConfigurationException e) {
return false;
} catch (SAXException e) {
return false;
} catch (Exception e) {
return false;
}
}
use of org.dom4j.io.SAXValidator in project OpenOLAT by OpenOLAT.
the class ItemFileResourceValidator method validateDocument.
private boolean validateDocument(Document in) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
SimpleErrorHandler errorHandler = new SimpleErrorHandler();
ItemContentHandler contentHandler = new ItemContentHandler();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setEntityResolver(new IMSEntityResolver());
reader.setErrorHandler(errorHandler);
reader.setContentHandler(contentHandler);
SAXValidator validator = new SAXValidator(reader);
validator.validate(in);
return errorHandler.isValid() && contentHandler.isItem();
} catch (ParserConfigurationException e) {
return false;
} catch (SAXException e) {
return false;
} catch (Exception e) {
return false;
}
}
Aggregations