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
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);// www. j a va 2 s.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 w w w .j a v a 2s . 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 { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder(); Document document = parser.parse(new File("NewFile.xml")); Schema schema = schemaFactory.newSchema(new File("NewFile.xsd")); Validator validator = schema.newValidator(); validator.validate(new DOMSource(document)); }
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 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;// www. ja va 2 s. 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);/*from w w 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 { 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);// w w w . j a v a 2 s . c om }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<?xml version='1.0'?><test><test2></test2></test>"; String schemaString = // "<?xml version='1.0'?>"// + "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' elementFormDefault='unqualified' attributeFormDefault='unqualified'>"// + "<xsd:element name='test' type='Test'/>"// + "<xsd:element name='test2' type='Test2'/>"// + "<xsd:complexType name='Test'>"// + "<xsd:sequence>"// + "<xsd:element ref='test2' minOccurs='1' maxOccurs='unbounded'/>"// + "</xsd:sequence>"// + "</xsd:complexType>"// + "<xsd:simpleType name='Test2'>"// + "<xsd:restriction base='xsd:string'><xsd:minLength value='1'/></xsd:restriction>"// + "</xsd:simpleType>"// + "</xsd:schema>"; Source schemaSource = new StreamSource(new StringReader(schemaString)); Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaSource); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true);//from w w w .j av a2s. c o m factory.setSchema(schema); SAXParser parser = factory.newSAXParser(); MyContentHandler handler = new MyContentHandler(); parser.parse(new InputSource(new StringReader(xml)), handler); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser parser = null;// w w w . j a v a 2 s .c om 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); }