List of usage examples for javax.xml.validation SchemaFactory newSchema
public abstract Schema newSchema(Source[] schemas) throws SAXException;
From source file:Main.java
public static void main(String[] argv) throws Exception { String schemaLang = "http://www.w3.org/2001/XMLSchema"; SchemaFactory factory = SchemaFactory.newInstance(schemaLang); Schema schema = factory.newSchema(new StreamSource("sample.xsd")); Validator validator = schema.newValidator(); validator.validate(new StreamSource("sample.xml")); }
From source file:Main.java
public static void main(String args[]) throws Exception { String language = XMLConstants.W3C_XML_SCHEMA_NS_URI; SchemaFactory factory = SchemaFactory.newInstance(language); Schema schema = factory.newSchema(new File("yourSchema")); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setSchema(schema);//from www .j a v a 2s. c o m SAXParser parser = spf.newSAXParser(); // parser.parse(...); }
From source file:Main.java
public static void main(String[] args) throws Exception { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new File("customer.xsd")); JAXBContext jc = JAXBContext.newInstance(Customer.class); Customer customer = new Customer(); // populate the customer object JAXBSource source = new JAXBSource(jc, customer); schema.newValidator().validate(source); }
From source file:Main.java
public static void main(String[] args) throws Exception { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new File("data.xsd")); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);//from ww w . ja v a 2 s. c o m dbf.setSchema(schema); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(new File("data.xml")); Element result = document.getElementById("abc"); System.out.println(result); }
From source file:Main.java
public static void main(String[] args) throws Exception { Customer customer = new Customer(); customer.setName("abcabcabcabcabcabcabc"); customer.getPhoneNumbers().add(new PhoneNumber()); customer.getPhoneNumbers().add(new PhoneNumber()); customer.getPhoneNumbers().add(new PhoneNumber()); customer.getPhoneNumbers().add(new PhoneNumber()); customer.getPhoneNumbers().add(new PhoneNumber()); customer.getPhoneNumbers().add(new PhoneNumber()); customer.getPhoneNumbers().add(new PhoneNumber()); customer.getPhoneNumbers().add(new PhoneNumber()); customer.getPhoneNumbers().add(new PhoneNumber()); customer.getPhoneNumbers().add(new PhoneNumber()); JAXBContext jc = JAXBContext.newInstance(Customer.class); JAXBSource source = new JAXBSource(jc, customer); SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new File("customer.xsd")); Validator validator = schema.newValidator(); validator.setErrorHandler(new MyErrorHandler()); validator.validate(source);/*from w w w . j ava 2s. c o m*/ }
From source file:MainClass.java
public static void main(String[] args) throws IOException { File documentFile = new File(args[0]); File schemaFile = new File(args[1]); SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = null;//from w w w .ja va 2s. c om try { schema = factory.newSchema(schemaFile); } catch (SAXException e) { fail(e); } Validator validator = schema.newValidator(); SAXSource source = new SAXSource(new InputSource(new FileReader(documentFile))); try { validator.validate(source); } catch (SAXException e) { fail(e); } }
From source file:MarshalValidation.java
public static void main(String[] args) throws Exception { Person p = new Person(); p.setFirstName("B"); p.setLastName("H"); JAXBContext context = JAXBContext.newInstance(Person.class); Marshaller marshaller = context.createMarshaller(); SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new File("person.xsd")); marshaller.setSchema(schema);/* ww w . ja v a 2 s. c o m*/ marshaller.setEventHandler(new ValidationEventHandler() { public boolean handleEvent(ValidationEvent event) { System.out.println(event); return false; } }); marshaller.marshal(p, System.out); }
From source file:Main.java
public static void main(String[] args) throws Exception { 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); Validator validator = schema.newValidator(); 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); }
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 ww .j a v a2 s .co 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(); } }