Actually Werner, I discovered that it's pretty easy to validate an XML document, using the javax.xml.validation classes. And in my particular case, I just want to validate it after marshalling, not unmarshall it.

FWIW, here is my validation code:

import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

…

SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema";);

InputStream schemaInputStream = getClass().getResourceAsStream(“myschema.xsd”);
Schema schema = factory.newSchema(new StreamSource(schemaInputStream));

Validator xmlValidator = schema.newValidator();

String xml = "xml document contents go here”;
StreamSource xmlSource = new StreamSource(new StringReader(xml));

try {
xmlValidator.validate(xmlSource);

} catch (SAXException e) {
// handle validation errors
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

And if you want to get *all* validation errors, you can use your own ErrorHandler implementation:

xmlValidator.setErrorHandler(new MyErrorHandler());

Where MyErrorHandler is defined as:

class MyErrorHandler extends org.xml.sax.helpers.DefaultHandler {
public List<SAXParseException> errors = new ArrayList();

public void error(SAXParseException e) throws SAXException {
// add the error to our list but don't throw it, so the validator can continue
errors.add(e);
}
}

You could also implement org.xml.sax.ErrorHandler directly, adding warning() and fatalError() methods.

-Steve


On Jun 4, 2009 2:40am, Werner Guttmann <[email protected]> wrote:
Steve,



there's one (or more) existing Jira issues out there which is about

adding exactly this feature. As far as I remember, the code base holds a

50% solution, but there's still some areas to be added/coded to enable

the described behaviour.



Fell free to have a look at the code base, the Jira issues and let us

know whether you'd like to contribute.



Cheers

Werner



Steve Kingsland wrote:

> I have configured my castor.properties file so that I'm now getting my XML

> documents validated against the Schema when I unmarshall, by adding these

> lines:

>

> org.exolab.castor.parser.namespaces=true

>

> org.exolab.castor.sax.features=http://xml.org/sax/features/validation,\

>

> http://apache.org/xml/features/validation/schema,\

>

> http://apache.org/xml/features/validation/schema-full-checking

>

> But I notice that I get a “MarshalException” when the document fails to

> validate, instead of a “ValidationException”. For example:

>

> *org.exolab.castor.xml.MarshalException*: cvc-enumeration-valid: Value

> 'PFPa' is not facet-valid with respect to enumeration '[PFP, MTBS]'. It must

> be a value from the enumeration.{File: [not available]; line: 6; column: 54}

>

> at

> org.exolab.castor.xml.Unmarshaller.convertSAXExceptionToMarshalException(*

> Unmarshaller.java:794*)

>

> at org.exolab.castor.xml.Unmarshaller.unmarshal(*Unmarshaller.java:760

> *)

>

> at org.exolab.castor.xml.Unmarshaller.unmarshal(*Unmarshaller.java:626

> *)

>

> at org.CastorTest.validateXml(*CmhToStamps.java:378*)

>

> at org.CastorTest.testMarshallGeneric(*CmhToStamps.java:242*)

>

> at org.CastorTest.main(*CmhToStamps.java:55*)

>

> Caused by: *org.xml.sax.SAXParseException*: cvc-enumeration-valid: Value

> 'PFPa' is not facet-valid with respect to enumeration '[PFP, MTBS]'. It must

> be a value from the enumeration.

>

> at

> org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown

> Source)

>

> at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)

>

> ...

>

> (etc.)

>

> That's fine, but ideally I'd like the XML parser to inform me of **all**

> validation errors, not just the first one it comes across. And looking at

> the ValidationException class, I see a getNext() method which looks like it

> was meant to support this feature! So how do I get Castor to throw a

> ValidationException when my XML document fails validation?

>

> Or another way of putting it is, is there a way for me to validate XML

> documents with Castor so that I find out about **all** problems, and not

> just the first one? I'm ok changing XML parsers, if that's what's needed.

>

> -Steve

>

> ps – I'm using the latest version of Castor, and Xerces.

>



---------------------------------------------------------------------

To unsubscribe from this list, please visit:



http://xircles.codehaus.org/manage_email





Reply via email to