List of usage examples for org.dom4j.io SAXValidator SAXValidator
public SAXValidator(XMLReader xmlReader)
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 {/*from www.java2s . c o m*/ //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.olat.ims.qti.qpool.ItemFileResourceValidator.java
License:Apache License
private boolean validateDocument(Document in) { try {// w w w. ja v a2 s .c om 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 ww w . j a v a 2 s.co 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.//from w ww . j a va2s .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 {/* ww w . ja va2 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; }