Sorry for resurrecting this issue from 6 weeks ago. I tried the suggestion that Stephen gave to me for doing XSD validation on an input XML using the Castor XML Mapping method:
1) Instantiate/configure a SAX parser (specify the external-schema-location feature) 2) Instantiate/configure an Unmarshaller instance 3) Call createHandler() on the Unmarshaller object to get an UnmarshalHandler which implements ContentHandler and ErrorHandler 4) Set the parser's content handler to the UnmarshalHandler object 5) Set the parser's error handler to the UnmarshalHandler object 6) Parse the document 7) Call getObject on the UnmarshalHandler to retrieve the root object so the code looks something like this: UnmarshalHandler unmarshalHandler = unmarshaller.createHandler(); // Set the parser's content handler to the // UnmarshalHandler object xmlReader.setContentHandler(unmarshalHandler); // Set the parser's error handler to the // UnmarshalHandler object xmlReader.setErrorHandler(unmarshalHandler); // Parse the xml document xmlReader.parse(inputSource); This solution has worked for me. The only problem is that on my project we have a requirement to show all XSD validation errors for an input XML. The problem with Castor's Unmarshaller handler is that it only reports the first XSD validation error. So for example, if an XML document fails because of 3 different XSD validation errors, then Castor's Unmarshaller error handler only shows the first XSD validation error. Is there an easy way around this? How can I get it to give me all the XSD validation errors? Thanks. -Saladin --- Stephen Bash <[EMAIL PROTECTED]> wrote: > Werner and Saladin- > > Sorry to jump in at the end here, but there might be > an easy way > around this issue. If all you need is a custom > configured parser you > can instantiate your own parser and have Castor use > it: > > 1) Instantiate/configure a SAX parser (specify the > external-schema-location feature) > 2) Instantiate/configure an Unmarshaller instance > 3) Call createHandler() on the Unmarshaller object > to get an > UnmarshalHandler which implements ContentHandler and > ErrorHandler > 4) Set the parser's content handler to the > UnmarshalHandler object > 5) Set the parser's error handler to the > UnmarshalHandler object > 6) Parse the document > 7) Call getObject on the UnmarshalHandler to > retrieve the root object > > Feel free to ignore me if I've completely missed the > mark, but I hope > that helps. > > Stephen > > > On 3/14/07, Werner Guttmann > <[EMAIL PROTECTED]> wrote: > > Okay, I don't think that what you want to achieve > is possible without a small code change to the > Unmarshaller. I just had a look at the JAXP API, and > it looks like I have to provide you with an option > to specify the > > > > > http://apache.org/xml/properties/schema/external-schemaLocation" > > > > feature of the JAXP Parser instance used > internally by Castor. If this is an option for you, > please create a new Jira issue at > > > > http://jira.codehaus.org/browse/CASTOR > > > > and attach the relevant fragments of this email > conversation. > > > > Werner > > > > > -----Ursprüngliche Nachricht----- > > > Von: S. Sharif [mailto:[EMAIL PROTECTED] > > > Gesendet: Dienstag, 13. März 2007 21:00 > > > An: [email protected] > > > Betreff: Re: AW: AW: [castor-user] How to do XSD > validation when the code > > > was not generated by the Castor code generator > > > > > > > > > Yes it has a namespace declaration. The > following is > > > an example of how the input xml looks like: > > > > > > <?xml version="1.0" encoding="UTF-8"?> > > > <Document > > > > xmlns="http://www.nproj.com/japps/AcceptRunRequest"> > > > <CRITERIAINPUT_LIST> > > > <CRITERIAINPUT> > > > <CRITERIAID>1</CRITERIAID> > > > </CRITERIAINPUT> > > > </CRITERIAINPUT_LIST> > > > > > > [ ... remaining xml was omitted for brevity ...] > > > > > > </Document> > > > > > > > > > In my project each Java service has one XSD > schema > > > file for the input xml that it is expecting. So > the > > > above is an example for one input xml for one > > > particular service. The input xml for the other > Java > > > services have a very similar structure with the > > > namespace decalred in the root tag as in the > example > > > above. > > > -Saladin > > > > > > --- Werner Guttmann <[EMAIL PROTECTED]> > wrote: > > > > > > > Well, let's do a few things first. Can you > show me > > > > what the XML that > > > > needs to be validated looks like ? Does it > have > > > > namespace declarations, > > > > etc ? > > > > > > > > Werner > > > > > > > > S. Sharif wrote: > > > > > I am not sure I understand what you mean. > Do you > > > > have > > > > > any basic or short code example that can > give me > > > > the > > > > > basic idea of what you mean? > > > > > Thanks. > > > > > > > > > > > > > > > --- Werner Guttmann > <[EMAIL PROTECTED]> > > > > > wrote: > > > > > > > > > >> You can always use an EntityResolver and > repoint > > > > a > > > > >> request for a 'global' XML schema > identifier to > > > > any > > > > >> file or InputSource convenient to you. > > > > >> > > > > >> Werner > > > > >> > > > > >>> -----Ursprüngliche Nachricht----- > > > > >>> Von: S. Sharif > [mailto:[EMAIL PROTECTED] > > > > >>> Gesendet: Dienstag, 13. März 2007 17:27 > > > > >>> An: [email protected] > > > > >>> Betreff: Re: AW: [castor-user] How to do > XSD > > > > >> validation when the code was > > > > >>> not generated by the Castor code generator > > > > >>> > > > > >>> > > > > >>> Thank for the link. But the problem is > that in > > > > >> the > > > > >>> example it validates an xml document > against the > > > > >> XSD > > > > >>> file specified in xsi:schemaLocation > attribute > > > > of > > > > >> the > > > > >>> root xml tag. > > > > >>> > > > > >>> The xml documents that get submitted to my > Java > > > > >>> service do not have the XSD schemaLocation > > > > >> attribute > > > > >>> specified. When I receive the XML > document, my > > > > >> Java > > > > >>> code then needs to perform XSD validation > based > > > > on > > > > >> a > > > > >>> predefined path for the XSD file. So I > need to > > > > >> set > > > > >>> the XSD file location programmatically, if > you > > > > >> know > > > > >>> what i mean. > > > > >>> > > > > >>> I mean I need to do something like this: > > > > >>> > > > > >>> > > > > >>> Mapping myMap = new Mapping(); > > > > >>> myMap.loadMapping( "po1Map.xml" ); > > > > >>> > > > > >>> Unmarshaller um1 = new Unmarshaller( myMap > ); > > > > >>> > > > > > um1.setSchemaLocation("http://www.example.com/PO1 > > > > >>> /project/schema/po1.xsd"); > > > > >>> PurchaseOrder po1 = > > > > >> (PurchaseOrder)um1.unmarshal(new > > > > >>> FileReader(filename)); > > > > >>> > > > > >>> > > > > >>> Note: this assumes that the xsd file > po1.xsd is > > > > >>> located in the /project/schema/ folder. > > > > >>> > > > > >>> > > > > >>> The problem is that there is no > > > > >> setSchemaLocation() > > > > >>> method in the Unmarshaller class. So is > there > > > > any > > > > >>> other way to do this? > > > > >>> > > > > >>> Thanks. > > > > >>> > > > > >>> > > > > >>> --- Werner Guttmann > <[EMAIL PROTECTED]> > > > > >>> wrote: > > > > >>> > > > > > > > > > > > > > http://www.castor.org/how-to-enable-xml-validation.html > > > > >>>> should provide you with answers to your > > > > >> question. > > > > >>>> Werner > > > > >>>> > > > > >>>>> -----Ursprüngliche Nachricht----- > > > > >>>>> Von: S. Sharif > [mailto:[EMAIL PROTECTED] > > > > >>>>> Gesendet: Dienstag, 13. März 2007 02:23 > > > > >>>>> An: [email protected] > > > > >>>>> Betreff: [castor-user] How to do XSD > > > > >> validation > > > > >>>> when the code was not > > > > >>>>> generated by the Castor code generator > > > > >>>>> > > > > >>>>> > > > > >>>>> Hi, > > > > >>>>> > > > > >>>>> I am not using the Castor code generator > to > > > > >>>> generate > > > > >>>>> my Java classes. Instead I have my own > java > > > > >>>> classes > > > > >>>>> that I am marshalling and unmarshalling > > > > >> to/from > > > > >>>> them > > > > >>>>> using a Castor mapping file. > > > > >>>>> > > > > >>>>> But now I am faced with the problem of > how to > > > > >>>> perform > > > > >>>>> the XSD validation. I know that the > Castor > > > > >> code > > > > >>>>> generator generates each Java class with > the > > > > >>>> following > > > > >>>>> code: > > > > >>>>> > > > > >>>>> /** > > > > >>>>> * > > > > >>>>> * > > > > >>>>> * @throws > > > > >>>> org.exolab.castor.xml.ValidationException > > > > >>>>> if this > > > > >>>>> * object is an invalid instance > according to > > > > >> the > > > > >>>>> schema > > > > >>>>> */ > > > > >>>>> public void validate() > > > > >>>>> throws > > > > >> org.exolab.castor.xml.ValidationException { > > > > >>>>> org.exolab.castor.xml.Validator > validator > > > > >> = > > > > >>>> new > > > > >>>>> org.exolab.castor.xml.Validator(); > > > > >>>>> validator.validate(this); > > > > >>>>> } > > > > >>>>> > > > > >>>>> > > > > >>>>> and that this code performs the XSD > validation > > > > >> on > > > > >>>> the > > > > >>>>> Java object using the ClassDescriptors > and > > > > >>>>> FieldDescriptors that were generated for > that > > > > >>>> object. > > > > >>>>> But now how do I perform XSD validation > when I > > > > >>>> have my > > > > >>>>> own Java classes (that were not > generated by > > > > >>>> Castor). > > > > >>>>> Is there some Castor API that I can call > and > > > > >> pass > > > > >>>> to > > > > >>>>> it the path to the XSD file, the Java > object > > > > >> that > > > > >>>> I > > > > >>>>> want to validate, and also the Castor > mapping > > > > >>>> file, > > > > >>>>> and then it performs the validation? > > > > >>>>> Or how should I go about doing this? > > > > >>>>> > > > > >>>>> Thanks. > > > > >>>>> > > > > >>>>> -Saladin > > > > >>>>> > > > > >>>>> > > > > >>>>> > > > > > > > > > > > > > ********************************************************** > > > > >>>>> * Saladin Sharif > > > > >>>>> * e-mail: [EMAIL PROTECTED] > > > > >>>>> * Visit homepage @ > > > > >>>> http://gaia.ecs.csus.edu/~sharifs > > > > > > > > > > > > > ********************************************************** > > > > >>>>> > > > > >>>>> > > > > >>>>> > > > > > > > > > > > > > __________________________________________________________________________ > > > > >>>>> __________ > > > > >>>>> Don't pick lemons. > > > > >>>>> See all the new 2007 cars at Yahoo! > Autos. > > > > >>>>> http://autos.yahoo.com/new_cars.html > > > > >>>>> > > > > >>>>> > > > > > > > > > > > > > --------------------------------------------------------------------- > > > > >>>>> To unsubscribe from this list please > visit: > > > > >>>>> > > > > >>>>> > http://xircles.codehaus.org/manage_email > > > > >>>> > > > > >>>> > > > > > > > > > > > > > --------------------------------------------------------------------- > > > > >>>> To unsubscribe from this list please > visit: > > > > >>>> > > > > >>>> > http://xircles.codehaus.org/manage_email > > > > >>>> > > > > >>>> > > > > >>> > > > > >>> > > > > >>> > > > > >>> > > > > > > > > > > > > > __________________________________________________________________________ > > > > >>> __________ > > > > >>> Expecting? Get great news right away with > email > > > > >> Auto-Check. > > > > >>> Try the Yahoo! Mail Beta. > > > > >>> > > > > > > > > > > > > > http://advision.webevents.yahoo.com/mailbeta/newmail_tools.html > > > > >>> > > > > > > > > > > > > > --------------------------------------------------------------------- > > > > >>> To unsubscribe from this list please > visit: > > > > >>> > > > > >>> > http://xircles.codehaus.org/manage_email > > > > >> > > > > >> > > > > > > > > > > > > > --------------------------------------------------------------------- > > > > >> To unsubscribe from this list please visit: > > > > >> > > > > >> > http://xircles.codehaus.org/manage_email > > > > >> > > > > >> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > __________________________________________________________________________ > > > __________ > > > > > Finding fabulous fares is fun. > > > > > Let Yahoo! FareChase search your favorite > travel > > > > sites to find flight and hotel bargains. > > > > > > http://farechase.yahoo.com/promo-generic-14795097 > > > > > > > > > > > > > > > > > > --------------------------------------------------------------------- > > > > > To unsubscribe from this list please visit: > > > > > > > > > > http://xircles.codehaus.org/manage_email > > > > > > > > > > > > > > > > > > > > > --------------------------------------------------------------------- > > > > To unsubscribe from this list please visit: > > > > > > > > http://xircles.codehaus.org/manage_email > > > > > > > > > > > > > > > > > > > > > > > > __________________________________________________________________________ > > > __________ > > > The fish are biting. > > > Get more visitors on your site using Yahoo! > Search Marketing. > > > > http://searchmarketing.yahoo.com/arp/sponsoredsearch_v2.php > > > > > > > --------------------------------------------------------------------- > > > To unsubscribe from this list please visit: > > > > > > http://xircles.codehaus.org/manage_email > > > > > > > --------------------------------------------------------------------- > > To unsubscribe from this list please visit: > > > > http://xircles.codehaus.org/manage_email > > > > > ____________________________________________________________________________________ TV dinner still cooling? Check out "Tonight's Picks" on Yahoo! TV. http://tv.yahoo.com/ --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email

