List of usage examples for javax.xml.validation Validator validate
public void validate(Source source) throws SAXException, IOException
From source file:Main.java
/** * This method will validate a non-niem xml instance. * /*www . ja v a 2s . com*/ * @param xsdPath - this is a relative path to an xsd, typically in OJB_Utilies or in the enclosing project * @param xml - This is the XML document as a string * @throws Exception - typically a validation exception or a file path exception */ public static void validateInstanceNonNIEMXsd(String xsdPath, String xml) throws Exception { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new File(xsdPath)); Validator validator = schema.newValidator(); validator.validate(new StreamSource(new StringReader(xml))); }
From source file:Main.java
public static boolean doesXMLMatchXSD(String xmlUrl) throws SAXException, FileNotFoundException, IOException { boolean doesMatchXSD = false; SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); SAXSource sourceXSD = new SAXSource(new InputSource(new FileInputStream(new File(xsdUrl)))); Schema schema = factory.newSchema(sourceXSD); Validator validator = (Validator) schema.newValidator(); validator.validate(new StreamSource(new File(xmlUrl))); doesMatchXSD = true;//from w ww. java2s . c o m return doesMatchXSD; }
From source file:Main.java
private static String isXmlPassingSchemaValidation(InputStream xml, InputStream xsd) throws IOException { Source xmlSource = new StreamSource(xml); try {//from w ww . j a v a 2 s . c o m SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new StreamSource(xsd)); Validator validator = schema.newValidator(); validator.validate(xmlSource); return null; } catch (SAXException e) { return e.toString(); } }
From source file:Main.java
public static void validate(Schema schema, Document document) { Validator validator = schema.newValidator(); try {//from ww w.j av a2s. c o m validator.validate(new DOMSource(document)); } catch (Exception e) { throw new RuntimeException("Failed to validate document", e); } }
From source file:Main.java
public static void validateSChema(String xmlFile, String... xsdFiles) throws SAXException, IOException { Source[] schemas = new Source[xsdFiles.length]; for (int i = 0; i < schemas.length; i++) { schemas[i] = new StreamSource(xsdFiles[i]); }// w w w. ja v a2 s . c om SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Schema schema = schemaFactory.newSchema(schemas); Validator validator = schema.newValidator(); Source source = new StreamSource(xmlFile); validator.validate(source); }
From source file:Main.java
/** * Validates the given document with its schema. * /*from w ww. j av a2s . c o m*/ * @param document - The XML file contents * @param schemaFileStream - the inputStream of the schema content * @throws Exception */ public static void validateDocument(Document document, InputStream schemaFileStream) throws Exception { // create a SchemaFactory capable of understanding WXS schemas SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // load a WXS schema, represented by a Schema instance Source schemaFile = new StreamSource(schemaFileStream); Schema schema = factory.newSchema(schemaFile); // create a Validator instance, which can be used to validate an // instance document Validator validator = schema.newValidator(); // validate the DOM tree validator.validate(new DOMSource(document)); }
From source file:Main.java
public static boolean validateXML(String schemaPath, String xmlPath) { try {/*from w ww. j av a 2 s . co m*/ String schemaLang = "http://www.w3.org/2001/XMLSchema"; SchemaFactory factory = SchemaFactory.newInstance(schemaLang); // create schema by reading it from an XSD file: Schema schema = factory.newSchema(new StreamSource(schemaPath)); Validator validator = schema.newValidator(); // at last perform validation: validator.validate(new StreamSource(xmlPath)); } catch (Exception ex) { return false; } return true; }
From source file:Main.java
public static boolean validateXMLString(String xsdPath, String xml) { try {/*from w ww .ja va 2 s .com*/ SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new File(xsdPath)); Validator validator = schema.newValidator(); Source source = new StreamSource(new StringReader(xml)); validator.validate(source); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
From source file:Main.java
public static void validateXml(Node root, InputStream xsd) throws SAXException, IOException { try {// ww w . jav a 2 s.co m Source source = new StreamSource(xsd); Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(source); Validator validator = schema.newValidator(); validator.validate(new DOMSource(root)); } finally { closeStream(xsd); } }
From source file:Main.java
/** * Validate xml file using specific schema * @param sourceXmlBytes// www . ja va 2 s .c om * the byte array reading from xml file * @param schemaUrl * schema url used for validation * @return byte[] */ public static void validate(byte[] sourceXmlBytes, URL schemaUrl) throws IOException, SAXException { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); try { Schema schema = factory.newSchema(schemaUrl); Validator validator = schema.newValidator(); Source source = new StreamSource(new ByteArrayInputStream(sourceXmlBytes)); validator.validate(source); } catch (SAXException ex) { throw ex; } catch (IOException e) { throw e; } }