Example usage for javax.xml.validation SchemaFactory newInstance

List of usage examples for javax.xml.validation SchemaFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.validation SchemaFactory newInstance.

Prototype

public static SchemaFactory newInstance(String schemaLanguage) 

Source Link

Document

Lookup an implementation of the SchemaFactory that supports the specified schema language and return it.

Usage

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[] 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  ww  w.  java 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("data.xsd"));

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from w  w  w. j a v a 2  s . com*/
    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;/*from   w w  w.  j  a va 2 s .  c o m*/
    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);//w  ww  . j  a  v a2  s.c om
    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);/*from  ww w  .j a va  2s.  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);/*ww w. jav a2 s  .co m*/
    factory.setSchema(schema);
    SAXParser parser = factory.newSAXParser();
    MyContentHandler handler = new MyContentHandler();
    parser.parse(new InputSource(new StringReader(xml)), handler);

}