List of usage examples for org.dom4j.io SAXValidator validate
public void validate(Document document) throws SAXException
Document by writing it to a validating SAX Parser. From source file:news.Confirmation.java
public static void validateXMLByXSD() { String xmlFileName = "C:\\Users\\Andy\\Documents\\NetBeansProjects\\WebService\\build\\web\\XML\\HomeNews.xml"; String xsdFileName = "C:\\Users\\Andy\\Documents\\NetBeansProjects\\WebService\\web\\Schmea.xsd"; try {/*ww w.j av a2s . c om*/ //XML? XMLErrorHandler errorHandler = new XMLErrorHandler(); //? SAX ? SAXParserFactory factory = SAXParserFactory.newInstance(); //??? XML factory.setValidating(true); //????? XML ??? factory.setNamespaceAware(true); //??? SAXParser SAXParser parser = factory.newSAXParser(); //? SAXReader xmlReader = new SAXReader(); //??xml Document xmlDocument = (Document) xmlReader.read(new File(xmlFileName)); // XMLReader ? [url]http://sax.sourceforge.net/?selected=get-set[/url] 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:" + xsdFileName); //SAXValidator SAXValidator validator = new SAXValidator(parser.getXMLReader()); //????? validator.setErrorHandler(errorHandler); // validator.validate(xmlDocument); XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint()); //???? if (errorHandler.getErrors().hasContent()) { System.out.println("XMLXSD?"); writer.write(errorHandler.getErrors()); } else { System.out.println("Good! XMLXSD??"); } } catch (Exception ex) { System.out.println("XML: " + xmlFileName + " XSD:" + xsdFileName + "\n " + ex.getMessage()); ex.printStackTrace(); } }
From source file:org.dom4j.samples.validate.SAXValidatorDemo.java
License:Open Source License
protected void validate(String url, boolean validateOnParse) throws Exception { println("Parsing: " + url + " with validation mode: " + validateOnParse); XMLErrorHandler errorHandler = new XMLErrorHandler(); if (validateOnParse) { // validate as we parse SAXReader reader = new SAXReader(true); reader.setErrorHandler(errorHandler); try {/*from w ww .j av a2 s. co m*/ Document document = reader.read(url); println("Document: " + url + " is valid!"); } catch (DocumentException e) { println("Document: " + url + " is not valid"); println("Exception: " + e); } } else { // parse without validating, then do that later SAXReader reader = new SAXReader(); Document document = reader.read(url); println("Document URI: " + document.getName()); // now lets set a doc type if one isn't set DocumentType docType = document.getDocType(); if (docType == null) { println("Adding an NITF doc type"); document.addDocType("nitf", null, "nitf.dtd"); } // now lets validate try { SAXValidator validator = new SAXValidator(); validator.setErrorHandler(errorHandler); validator.validate(document); println("Document: " + url + " is valid!"); } catch (SAXException e) { println("Document: " + url + " is not valid"); println("Exception: " + e); } } // now lets output any errors as XML Element errors = errorHandler.getErrors(); if (errors.hasContent()) { XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint()); writer.write(errors); } }
From source file:org.olat.ims.qti.qpool.ItemFileResourceValidator.java
License:Apache License
private boolean validateDocument(Document in) { try {//from w w w .j a va 2 s . c o m 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; } }
From source file:org.openremote.modeler.service.ProtocolParser.java
License:Open Source License
/** * Read xml from file.//from w w w . jav a2 s . c o m * * @param file the file * * @return the document */ private Document readXmlFromFile(File file) { Document protocolDoc = null; SAXReader reader = new SAXReader(); XMLErrorHandler errorHandler = new XMLErrorHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); try { protocolDoc = reader.read(file); 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:" + this.getClass().getResource("/").getPath().toString() + PROTOCOL_XSD_FILE_NAME); SAXValidator validator = new SAXValidator(parser.getXMLReader()); validator.setErrorHandler(errorHandler); validator.validate(protocolDoc); XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint()); if (errorHandler.getErrors().hasContent()) { writer.write(errorHandler.getErrors()); throw new ParseProtocolException( "validate xml schema on File " + file.getAbsolutePath() + " fail."); } } catch (ParserConfigurationException e) { throw new ParseProtocolException( "Read xml From File " + file.getAbsolutePath() + " occur ParserConfigurationException.", e); } catch (SAXException e) { throw new ParseProtocolException( "Read xml From File " + file.getAbsolutePath() + " occur SAXException.", e); } catch (UnsupportedEncodingException e) { throw new ParseProtocolException( "Read xml From File " + file.getAbsolutePath() + " occur UnsupportedEncodingException.", e); } catch (IOException e) { throw new ParseProtocolException("Read xml From File " + file.getAbsolutePath() + " occur IOException.", e); } catch (DocumentException e) { throw new ParseProtocolException( "Read xml From File " + file.getAbsolutePath() + " occur DocumentException.", e); } return protocolDoc; }
From source file:org.openremote.modeler.service.TouchPanelParser.java
License:Open Source License
/** * Read xml from file./*ww w . ja v a 2 s . co m*/ * * @param file the file * * @return the document */ private Document readXmlFromFile(File file) { Document panelDoc = null; SAXReader reader = new SAXReader(); XMLErrorHandler errorHandler = new XMLErrorHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); try { panelDoc = reader.read(file); 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:" + this.getClass().getResource("/").getPath().toString() + PANEL_XSD_FILE_NAME); SAXValidator validator = new SAXValidator(parser.getXMLReader()); validator.setErrorHandler(errorHandler); validator.validate(panelDoc); XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint()); if (errorHandler.getErrors().hasContent()) { writer.write(errorHandler.getErrors()); throw new ParseTouchPanelException( "validate xml schema on File " + file.getAbsolutePath() + " fail."); } } catch (ParserConfigurationException e) { throw new ParseTouchPanelException( "Read xml From File " + file.getAbsolutePath() + " occur ParserConfigurationException.", e); } catch (SAXException e) { throw new ParseTouchPanelException( "Read xml From File " + file.getAbsolutePath() + " occur SAXException.", e); } catch (UnsupportedEncodingException e) { throw new ParseTouchPanelException( "Read xml From File " + file.getAbsolutePath() + " occur UnsupportedEncodingException.", e); } catch (IOException e) { throw new ParseTouchPanelException( "Read xml From File " + file.getAbsolutePath() + " occur IOException.", e); } catch (DocumentException e) { throw new ParseTouchPanelException( "Read xml From File " + file.getAbsolutePath() + " occur DocumentException.", e); } return panelDoc; }
From source file:org.talend.mdm.webapp.base.server.util.XmlUtil.java
License:Open Source License
public static boolean validateXMLByXSD(String inputXml, File xsdFile) { boolean isValidated = false; String xsdFileName = xsdFile.getAbsolutePath(); try {//from w w w . j a va 2 s .c o m XMLErrorHandler errorHandler = new XMLErrorHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); Document xmlDocument = DocumentHelper.parseText(inputXml); // [url]http://sax.sourceforge.net/?selected=get-set[/url] parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", //$NON-NLS-1$ "http://www.w3.org/2001/XMLSchema"); //$NON-NLS-1$ parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", "file:" + xsdFileName); //$NON-NLS-1$ //$NON-NLS-2$ SAXValidator validator = new SAXValidator(parser.getXMLReader()); validator.setErrorHandler(errorHandler); validator.validate(xmlDocument); XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint()); if (errorHandler.getErrors().hasContent()) { isValidated = false; logger.error("XML file validation failed! "); //$NON-NLS-1$ writer.write(errorHandler.getErrors()); } else { isValidated = true; if (logger.isDebugEnabled()) { logger.debug("XML file validation succeeded! "); //$NON-NLS-1$ } } } catch (Exception ex) { isValidated = false; logger.error("Failed to validate XML file through '" + xsdFileName, ex); //$NON-NLS-1$ } return isValidated; }