List of usage examples for javax.xml.validation SchemaFactory newInstance
public static SchemaFactory newInstance(String schemaLanguage)
From source file:Main.java
public static boolean validate(URL schemaFile, String xmlString) { boolean success = false; try {//from w ww . ja v a2 s. c om SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(schemaFile); Validator validator = schema.newValidator(); Source xmlSource = null; try { xmlSource = new StreamSource(new java.io.StringReader(xmlString)); validator.validate(xmlSource); System.out.println("Congratulations, the document is valid"); success = true; } catch (SAXException e) { e.printStackTrace(); System.out.println(xmlSource.getSystemId() + " is NOT valid"); System.out.println("Reason: " + e.getLocalizedMessage()); } } catch (Exception e) { e.printStackTrace(); } return success; }
From source file:Main.java
public static Schema getSchema(InputStream... inputs) throws SAXException { StreamSource[] streamSource = new StreamSource[inputs.length]; for (int i = 0; i < inputs.length; i++) { streamSource[i] = new StreamSource(inputs[i]); }/*from w w w.j a v a 2 s.c om*/ SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(streamSource); return schema; }
From source file:Main.java
/** * This method validate XML by input XML as String and XSD path to File. * * @param xml input XML as String//from ww w . ja va 2s . c om * @param xsdPath input XSD File Path * * @return true or false, valid or not */ public static boolean validateXMLByXSD(String xml, String xsdPath) { try { SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new URL(xsdPath)).newValidator() .validate(new StreamSource(new StringReader(xml))); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
From source file:Main.java
public static boolean xmlStringValidate(String xmlStr, String xsdPath) { boolean flag = false; try {/* w w w . j av a 2 s. com*/ SchemaFactory factory = SchemaFactory.newInstance(SCHEMALANG); File schemaLocation = new File(xsdPath); Schema schema = factory.newSchema(schemaLocation); Validator validator = schema.newValidator(); InputStream is = new ByteArrayInputStream(xmlStr.getBytes()); Source source = new StreamSource(is); try { validator.validate(source); flag = true; } catch (SAXException ex) { System.out.println(ex.getMessage()); } } catch (Exception e) { e.printStackTrace(); } return flag; }
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 a2s. com 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
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 . j a v a2 s . c o m*/ return doesMatchXSD; }
From source file:Main.java
/** * Validates XML document using XSD schema. * * @param document document to validate * @param pathToSchema path to document with XSD schema * @return {@code true} if document is valid *//* w w w.ja v a2 s .c om*/ public static boolean validate(Document document, String pathToSchema) { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new File(pathToSchema)); Validator validator = schema.newValidator(); validator.validate(new DOMSource(document)); return true; } catch (IOException | SAXException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Validate xml file using specific schema * @param sourceXmlBytes/*from w w w . ja v a2s . c o m*/ * 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; } }
From source file:Main.java
private static String isXmlPassingSchemaValidation(InputStream xml, InputStream xsd) throws IOException { Source xmlSource = new StreamSource(xml); try {/*from www .j av 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
/** * Generic method to Validate XML file while unmarshalling against their schema. * /*from w ww . j a v a2 s.c o m*/ * @param context * @param schemaFile * @param object * @return * @throws SAXException * @throws JAXBException */ public static Object validateAndUnmarshallXML(JAXBContext context, String schemaFile, InputStream fio) throws SAXException, JAXBException { if (context != null && (schemaFile != null && schemaFile.trim().length() > 0)) { Unmarshaller unMarshaller = context.createUnmarshaller(); SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // thread- safe unMarshaller.setSchema(sf.newSchema(new File(schemaFile))); // validate jaxb context against schema return unMarshaller.unmarshal(fio); } return null; }