XML validation with JDOM / JAXP
Hello,
I am trying to validate xml file against schema file. I decided to use JDOM and JAXP.
First question: is it a good choice?
I did a first attempt but not sure I have understood all what I am doing :(
First I have create a parser usinf org.jdom.*
SAXBuilder parser =new SAXBuilder
Then I built my document:
Document doc = parser.build(myFile)
These 2 steps are ok. But it does not do validation.
I saw in the JDOM documentation that I can set a validation flag to true. I did it. First problem, I got the following error from JDOMException: Document is invalid: no grammar found.
Is there a way to specify to the parser where to find the xsd file?
As I did not find answer to this question by myself, I tried implementing the Schema class from JAXP:
SAXBuilder parser =new SAXBuilder;
Document doc = parser.build(myFile);
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaFile);
ValidatorHandler vh = schema.newValidatorHandler();
SAXOutputter so =new SAXOutputter(vh);
so.output(doc);
It does the validation against the schema file specified but I am not really sure about what I am doing here. The last 2 commands are not clear to me :(
Then in my schema file, I have elements that have default value. I expected the following behavior: if the element is not in the xml file then the default will be used by the parser (and added in the tree). But it seems, it's not the case.
Any help/explanation will be really appreciated.
[1706 byte] By [
marlysaa] at [2007-10-3 3:57:22]

[nobr]I am trying to validate xml file against schema file. I decided to use JDOM and JAXP.
First question: is it a good choice?
If only schema validation is required use the validation API in JDK 5.0.
http://www-128.ibm.com/developerworks/xml/library/x-javaxmlvalidapi.html
http://java.sun.com/developer/technicalArticles/xml/validationxpath/
For validation with JDOM, the following validation application explains the procedure:import org.jdom.input.SAXBuilder;
import org.xml.sax.SAXException;import org.jdom.*;
import java.io.*;
public class JDOMValidator{
public void validateSchema(String SchemaUrl, String XmlDocumentUrl){
try{
SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser",true);<br/>saxBuilder.setValidation(true);
saxBuilder.setFeature("http://apache.org/xml/features/validation/schema",true);
saxBuilder.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
saxBuilder.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",SchemaUrl);
Validator handler=new Validator();
saxBuilder.setErrorHandler(handler);
saxBuilder.build(XmlDocumentUrl);
if(handler.validationError==true)
System.out.println("XML Document has Error:"+handler.validationError+""+
handler.saxParseException.getMessage());
else
System.out.println("XML Document is valid");
}catch(JDOMException jde){
}catch(IOException ioe){
}
}
private class Validator extends DefaultHandler{
public boolean validationError = false;
public SAXParseException saxParseException=null;
public void error(SAXParseException exception) throws SAXException{
validationError =true;
saxParseException=exception;
}
public void fatalError(SAXParseException exception) throws SAXException {
validationError = true;
saxParseException=exception;
}
public void warning(SAXParseException exception) throws SAXException{
}
}
public static void main(String[] argv){
String SchemaUrl=argv[0];StringXmlDocumentUrl=argv[1];
JDOMValidator validator=new JDOMValidator();
validator.validateSchema(SchemaUrl,XmlDocumentUrl);
}
}
[/nobr]