List of usage examples for javax.xml XMLConstants W3C_XML_SCHEMA_NS_URI
String W3C_XML_SCHEMA_NS_URI
To view the source code for javax.xml XMLConstants W3C_XML_SCHEMA_NS_URI.
Click Source Link
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 . j ava 2 s. com*/ 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// ww w. j av a2 s .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 ava2s . co 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 w w .j av a2 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
private static Schema readSchemaFromFile(File schemaFile) throws SAXException { return (SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)).newSchema(schemaFile); }
From source file:Main.java
/** * Generic method to Validate XML file while marshalling against their schema. * /*from www .j a va2 s. c om*/ * @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; }
From source file:Main.java
/** * Perform a schema validation/*from ww w . j ava 2 s .c o m*/ */ public static void validate(Source schema, Source document) throws Exception { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema sch = factory.newSchema(schema); Validator validator = sch.newValidator(); validator.validate(document); }
From source file:Main.java
/** * This method validate XML by input XML as String and XSD File. * * @param xml input XML as String/*from w ww. j a va 2 s . co m*/ * @param xsd input XSD File * * @return true or false, valid or not */ public static boolean validateXMLByXSD(String xml, File xsd) { try { SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(xsd).newValidator() .validate(new StreamSource(new StringReader(xml))); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
From source file:Main.java
/** * Validates the given document with its schema. * //from ww w . ja v a 2 s.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
/** * This method will validate a non-niem xml instance. * /*from w w w .ja v a 2s.co 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))); }