Hello everybody!
 
I started to use Xerces today to validate XML files agains XML schema(s). I am using DOM parser.
What I did is the following:
 

public Document validate(File xmlFile, String schemaLocation) throws SAXException, ParserConfigurationException, IOException {

    String funcName = this.className + " - .validate() - ";

    // Set the DocumentBuilderFactory system property

    System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

    // Create the dom factory

    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();

    // Set the namespace property

    domFactory.setNamespaceAware(true);

    // Set the schema language property to be used for validation

    domFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", JMSValidator.W3C_XML_SCHEMA);

    // Specify the XML schema document to be used for validation

    domFactory.setAttribute(JMSValidator.JAXP_SCHEMA_SOURCE, new File(schemaLocation));

    domFactory.setAttribute("http://xml.org/sax/features/validation", true);

    domFactory.setAttribute("http://apache.org/xml/features/validation/schema", true);

    domFactory.setAttribute("http://apache.org/xml/features/validation/schema/element-default",true);

    domFactory.setAttribute("http://apache.org/xml/features/validation/schema/augment-psvi", true);

    // Create the document builder

    DocumentBuilder builder = domFactory.newDocumentBuilder();

    // Create and register an error handler with the parser

    JMSErrorHandler parserErrorHandler = new JMSErrorHandler();

    builder.setErrorHandler(parserErrorHandler);

    // Create the schema factory

    SchemaFactory schemaFactory = SchemaFactory.newInstance(JMSValidator.W3C_XML_SCHEMA);

    //schemaFactory.setErrorHandler(new JMSErrorHandler());

    Source schemaFile = new StreamSource(new File(schemaLocation));

    Schema schema = schemaFactory.newSchema(schemaFile);

    // Create the validator

    Validator validator = schema.newValidator();

    // Add an error handler to the validator

    JMSErrorHandler errorHandler = new JMSErrorHandler();

    validator.setErrorHandler(errorHandler);

    // Parse the xml file

    Document doc = builder.parse(xmlFile);

    if ( parserErrorHandler.validationError == true ) {

        // errors occured during the parsing

            if (myLogger.isInfoEnabled()) {

                myLogger.info(funcName + "XML file is not valid");

                myLogger.info(parserErrorHandler.saxParseException.getMessage());

            }

            System.exit(1);

    } else {

        if (myLogger.isInfoEnabled()) {

            myLogger.info(funcName + "XML file is valid");

        }

    }

    // Create the dom source and destination

    // The destination will contain the doc augmented with the default attribute/element

    DOMSource source = new DOMSource(doc);

    DOMResult result = new DOMResult();

    // Validate and augment the source

    validator.validate(source, result);

    // Error checking

    if ( errorHandler.validationError == true ) {

        // errors occured during the parsing

            if (myLogger.isInfoEnabled()) {

                myLogger.info(funcName + "XML file is not valid");

                myLogger.info(errorHandler.saxParseException.getMessage());

                }

        System.exit(1);

    } else {

        if (myLogger.isInfoEnabled()) {

        myLogger.info(funcName + "XML file is valid");

        }

    }

TransformerFactory tFactory =

TransformerFactory.newInstance();

Transformer transformer = null;

try {

    transformer = tFactory.newTransformer();

} catch (TransformerConfigurationException e) {

     e.printStackTrace();

}

DOMSource domSource = new DOMSource((Document)result.getNode());

StreamResult streamResult = new StreamResult(System.out);

try {

    transformer.transform(domSource, streamResult);

} catch (TransformerException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// Return the augmented document

return (Document) result.getNode();

}
 
I expected to get an augmented document in the result object. But it's not the case. My XML schema contains default value for elements. Should it not be added in the result object after the validation?
 
Thx in advance.

Reply via email to