Code example for Schema

Methods: newValidator

0
 
        // Parsers from this factory will automatically validate against the 
        // associated schema 
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(xml));
        schema.newValidator().validate(new DOMSource(doc));
    } 
 
    /** 
     * Validate xml with xsd by sax. 
     *  
     * @param xsd the reader of xsd file. 
     * @param xml the reader of xml file. 
     * @throws Exception 
     */ 
    public void validateWithSax(Reader xsd, Reader xml) throws Exception {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser parser = null;
        spf.setNamespaceAware(true);
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        spf.setSchema(sf.newSchema(new SAXSource(new InputSource(xsd))));