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:ValidateStax.java
/** * @param args// ww w . ja va 2 s . c om * the command line arguments */ public static void main(String[] args) { try { // TODO code application logic here SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); System.out.println("schema factory instance obtained is " + sf); Schema schema = sf.newSchema(new File(args[0])); System.out.println("schema obtained is = " + schema); // Get a Validator which can be used to validate instance document against // this grammar. Validator validator = schema.newValidator(); // Validate this instance document against the Instance document supplied String fileName = args[1].toString(); String fileName2 = args[2].toString(); javax.xml.transform.Result xmlResult = new javax.xml.transform.stax.StAXResult( XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(fileName2))); javax.xml.transform.Source xmlSource = new javax.xml.transform.stax.StAXSource( getXMLEventReader(fileName)); // validator.validate(new StreamSource(args[1])); validator.validate(xmlSource, xmlResult); } catch (Exception ex) { ex.printStackTrace(); System.out.println("GET CAUSE"); ex.getCause().fillInStackTrace(); } }
From source file:Main.java
public static Schema loadSchema(URL schemaURL) throws SAXException { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); return sf.newSchema(schemaURL); }
From source file:Main.java
/** * @param schema// w ww . ja va 2 s .c om * @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
static boolean validateAgainstXSD(InputStream xml, InputStream xsd) { try {//from w w w . j a v a2s .c om SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new StreamSource(xsd)); Validator validator = schema.newValidator(); validator.validate(new StreamSource(xml)); return true; } catch (Exception ex) { return false; } }
From source file:Main.java
public static void validate(String xmlFileName, String schemaFileName) throws IOException, ParserConfigurationException, SAXException { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(new File(schemaFileName)); Validator validator = schema.newValidator(); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); DocumentBuilder parser = builderFactory.newDocumentBuilder(); Document document = parser.parse(new File(xmlFileName)); validator.validate(new DOMSource(document)); }
From source file:Main.java
public static boolean validateXMLString(String xsdPath, String xml) { try {//from w w w. j a v a 2 s .c om 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 boolean validateXMLSchema(String xsdPath, String xmlPath) { try {/*from w w w .j a va 2 s .c om*/ 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
public static boolean validate(URL schemaFile, String xmlString) { boolean success = false; try {/* ww w . j a va 2s . com*/ 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
/** * This method validate XML by input XML as String and XSD path to File. * * @param xml input XML as String//from w w w .ja v a 2s. c o m * @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 XPath getSchemaXPath() { XPath xPath = schemaXPathThreadLocal.get(); if (xPath == null) { xPath = XPathFactory.newInstance().newXPath(); xPath.setNamespaceContext(new NamespaceContext() { @Override/*from w ww . j a v a 2 s. com*/ public String getNamespaceURI(String prefix) { return XMLConstants.W3C_XML_SCHEMA_NS_URI; } @Override public String getPrefix(String namespaceURI) { return "xs"; } @Override public Iterator getPrefixes(String namespaceURI) { return Arrays.asList("xs").iterator(); } }); schemaXPathThreadLocal.set(xPath); } return xPath; }