List of usage examples for javax.xml.validation SchemaFactory newInstance
public static SchemaFactory newInstance(String schemaLanguage)
From source file:MainClass.java
public static void main(String args[]) throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser parser = null;/*w ww . j av a 2 s . c o m*/ spf.setNamespaceAware(true); try { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); spf.setSchema(sf.newSchema(new SAXSource(new InputSource(new StringReader(schemaString))))); parser = spf.newSAXParser(); } catch (SAXException e) { e.printStackTrace(System.err); System.exit(1); } catch (ParserConfigurationException e) { e.printStackTrace(System.err); System.exit(1); } MySAXHandler handler = new MySAXHandler(); System.out.println(schemaString); parser.parse(new InputSource(new StringReader(xmlString)), handler); }
From source file:ValidateStax.java
/** * @param args//from w w w . ja v a 2 s. c o m * 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
public static void validate(Document doc, URL xsdLocation) throws Exception { SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Schema schema = factory.newSchema(xsdLocation); Validator validator = schema.newValidator(); validator.validate(new DOMSource(doc)); }
From source file:Main.java
/** * @param schema//from ww w . ja va 2 s .c o 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
static boolean validateAgainstXSD(InputStream xml, InputStream xsd) { try {//from w ww. j a va 2 s .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 {/* w w w. j a v a2 s. c o m*/ 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 {/*w w w . 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
public static String validate(String xsdPath, String xmlPath) { String response = "OK"; try {// w ww . ja v a 2 s . c om SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); File schemaLocation = new File(xsdPath); Schema schema = factory.newSchema(schemaLocation); Validator validator = schema.newValidator(); Source source = new StreamSource(xmlPath); Result result = null; validator.validate(source, result); } catch (Exception e) { response = e.getMessage(); } return response; }