List of usage examples for javax.xml.validation SchemaFactory newSchema
public abstract Schema newSchema(Source[] schemas) throws SAXException;
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 *///from w ww. j a va 2 s . co m 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
public static boolean validateXMLSchema(String xsdPath, String xmlPath) { try {/*from w ww . j ava 2s. c o m*/ 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 File(xmlPath))); } catch (IOException e) { System.out.println("Exception: " + e.getMessage()); return false; } catch (SAXException e) { System.out.println("Exception: " + e.getMessage()); return false; } return true; }
From source file:Main.java
/** * @param schema//from w ww. ja va2 s. co m * @return */ private static Schema parseSchema(File schema) { Schema parsedSchema = null; SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); try { parsedSchema = sf.newSchema(schema); } catch (SAXException e) { // TODO Auto-generated catch block System.out.println("Problems parsing schema " + schema.getName()); e.printStackTrace(); } return parsedSchema; }
From source file:Main.java
/** * This method will validate a non-niem xml instance. * /* w w w . ja v a2 s . c o m*/ * @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
private static String isXmlPassingSchemaValidation(InputStream xml, InputStream xsd) throws IOException { Source xmlSource = new StreamSource(xml); try {/*from w w w . j a va 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
/** * Validate xml file using specific schema * @param sourceXmlBytes//from ww w.j a va2s . co 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
public static boolean validate(URL schemaFile, String xmlString) { boolean success = false; try {/*from w ww . jav a 2 s . co m*/ 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(URL schemaURL) { Schema schema = null;/*from w w w .ja va2 s. c o m*/ try { if (schemaURL != null) { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schema = sf.newSchema(schemaURL); } } catch (Exception e) { throw new RuntimeException("Failed to create shcemma from URL " + schemaURL, e); } return schema; }
From source file:Main.java
/** * Generic method to Validate XML file while unmarshalling against their schema. * //from w w w .j av a 2 s . c om * @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; }
From source file:Main.java
/** * Generic method to Validate XML file while marshalling against their schema. * /*from w ww .j a v a 2 s .c o m*/ * @param context * @param schemaFile * @param object * @return * @throws SAXException * @throws JAXBException */ public static String validateAndMarshallXML(JAXBContext context, String schemaFile, Object object) throws SAXException, JAXBException { String xmlFormOfBean = null; if (context != null && (schemaFile != null && schemaFile.trim().length() > 0)) { Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // thread- safe marshaller.setSchema(sf.newSchema(new File(schemaFile))); // validate jaxb context against schema ByteArrayOutputStream sos = new ByteArrayOutputStream(); // for XML output into string marshaller.marshal(object, sos); xmlFormOfBean = sos.toString(); } return xmlFormOfBean; }