Hi Simon

We do this to, as our xsd performs validation that can't (as far as I
know) be done in Castor such as ID/IDREF etc.
We keep the xsd in a jar with the rest of the app which makes it easier
to find. 
All you need to do then is create an EntityResolver which locates the
xsd file for xerces or whatever parser you are using. I know there are
other ways of finding the xsd but this works just fine.

Code required (Using JDOM):

SAXBuilder builder = new SAXBuilder(true);
builder.setEntityResolver(new MenuEntityResolver());
//This will throw an exception if xsd validation fails.
Document doc = builder.build(aStream); 


The EntityResolver class:

import org.xml.sax.*;
import uk.co.netdev.fme.LogUtil;

public class MenuEntityResolver implements EntityResolver {

  public InputSource resolveEntity(String publicId, String systemId) {
      return new
InputSource(MenuEntityResolver.class.getResourceAsStream("fme.xsd"));  
  }
   
}
Hope this helps.

Regards
John
  
 
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
Sent: 22 June 2005 15:53
To: [email protected]
Subject: [castor-user] [XML] Validate against XSD when unmarshalling

Hi,

I'd like to validate the XML Message i recieve against the XSD the Data
Model was generated from. 

Let's say i have the following code:

Foo foo = Unmarshaller.unmarshall(Foo.class, aReader);

The Foo class was generated using the SourceGenerator from foo.xsd. How
can i validate the XML agains Foo.xsd?



Help is greatly appreciated.

Cheers,
Simon


-------------------------------------------------
If you wish to unsubscribe from this list, please 
send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------






-------------------------------------------------
If you wish to unsubscribe from this list, please 
send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------

Reply via email to