Here is the code I use to parse and validate my XML files:

// Set the DocumentBuilderFactory system property
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
System.setProperty("javax.xml.validation.SchemaFactory:http://www.w3.org
/2001/XMLSchema", "org.apache.xerces.jaxp.validation.XMLSchemaFactory");

// Create the dom factory
DocumentBuilderFactory domFactory =
DocumentBuilderFactory.newInstance();
                
// Set the namespace property
domFactory.setNamespaceAware(true);
                
// Ignore the comments
domFactory.setIgnoringComments(true);

// Eliminate whitespace in element content
domFactory.setIgnoringElementContentWhitespace(true);
domFactory.setCoalescing(true);
                                
// Set the schema language property to be used for validation
domFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaL
anguage", XMLConstants.W3C_XML_SCHEMA_NS_URI);  
                
// Set validation feature for the document builder factory
domFactory.setFeature("http://xml.org/sax/features/validation";, true);
domFactory.setFeature("http://apache.org/xml/features/validation/schema";
, true);
domFactory.setFeature("http://apache.org/xml/features/validation/schema/
element-default",true);

// Create the catalog           
XMLCatalogResolver catalog = new XMLCatalogResolver();
String[] catList = {"schemas\\Catalog.xml"};
catalog.setCatalogList(catList);
catalog.setPreferPublic(true);
                
// Add the catalog to the document builder factory
domFactory.setAttribute("http://apache.org/xml/properties/internal/entit
y-resolver", catalog);
                
// Create the document builder
DocumentBuilder builder = domFactory.newDocumentBuilder();
                
// Parse the xml file
Document doc = null;
if (xml instanceof File) {
        doc = builder.parse((File)xml);
} else if (xml instanceof String) {
        doc = builder.parse(new InputSource(new
StringReader((String)xml)));
} 
                
// Create the schema factory
SchemaFactory schemaFactory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                
// Create the schema object
Schema schema = schemaFactory.newSchema();
                
// Create the validator
Validator validator = schema.newValidator();

// Add the catalog to the validator to resolve external resouce location
validator.setProperty("http://apache.org/xml/properties/internal/entity-
resolver", catalog);
                
// Add an error handler to the validator
JMSErrorHandler errorHandler = new JMSErrorHandler();
validator.setErrorHandler(errorHandler);                

// 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 - Got a
validation error");
        
myLogger.info(errorHandler.saxParseException.getMessage());
        }
        System.exit(1);
}  else {
        if (myLogger.isInfoEnabled()) {
                myLogger.info(funcName + "XML file is valid");
        }
}
        
// Display the augmented tree
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) {
        e.printStackTrace();
}
                 
I am quite new in this area so if you find something strange or
incorrect, please tell me.

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to