Stephen,
I tried your suggestion, but I get the following
error:
org.xml.sax.SAXException: unable to find
FieldDescriptor for 'CRITERIAID' in ClassDescriptor of
CRITERIAINPUT
at
org.exolab.castor.xml.UnmarshalHandler.startElement(UnmarshalHandler.java:1924)
at
org.exolab.castor.xml.UnmarshalHandler.startElement(UnmarshalHandler.java:1362)
at
org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown
Source)
at
org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown
Source)
I followed the same steps that you outlined, except
for setting the xsd validation (i.e. specifing the
external-schema-location feature on the SAX parser).
I did not do that one step, as I wanted to make sure
that the Unmarshalling works with using the SAX parser
and the UnmarshalHandler before even trying to do any
XSD schema validation.
The following is the code that I tried:
// load castor mapping file
Mapping map = new Mapping();
map.loadMapping(mappingPath);
// Create an instance of Castor's unmarshaller
Unmarshaller unmarshaller = new
Unmarshaller(RunDvpRequestVO.class);
unmarshaller.setMapping(map);
// Create a Reader to the input file that is to be
unmarshalled from
Reader reader = new FileReader(inputFilePath);
// Create content handler for Castor's Unmarshaller
UnmarshalHandler unmarshalHandler =
unmarshaller.createHandler();
XMLReader xmlReader = new
org.apache.xerces.parsers.SAXParser();
xmlReader.setContentHandler(unmarshalHandler);
xmlReader.setErrorHandler(unmarshalHandler);
xmlReader.parse(new InputSource(reader));
Object obj = unmarshalHandler.getObject();
The exception gets thrown when the
xmlReader.parse(...) method is called.
Note: just to make sure that I was not getting the
error because of a problem with the input xml or the
Castor mapping file, I tried unmarshalling using
straight Castor unmarshalling code (see below) and it
works fine with any errors:
// load castor mapping file
Mapping map = new Mapping();
map.loadMapping(mappingPath);
// Create an instance of Castor's unmarshaller
Unmarshaller unmarshaller = new
Unmarshaller(RunDvpRequestVO.class);
unmarshaller.setMapping(map);
// Create a Reader to the input file to unmarshal from
Reader reader = new FileReader(inputFilePath);
// Unmarshall the input xml into a value object
RunDvpRequestVO runDvpRequest = (RunDvpRequestVO)
unmarshaller.unmarshal(reader);
-Saladin
--- Werner Guttmann <[EMAIL PROTECTED]> wrote:
> Actually, didn't know about this .. ;-). Which
> indicates that this
> should be documented somewhere ... e.g. XML HOW-TOs.
> And on top of that,
> maybe a setParser(SAXParser) method should be added
> to Unmarshaller.
>
> Werner
>
> Stephen Bash 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
> >>
> >>
>
>
>
---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
> http://xircles.codehaus.org/manage_email
>
>
**********************************************************
* Saladin Sharif
* e-mail: [EMAIL PROTECTED]
* Visit homepage @ http://gaia.ecs.csus.edu/~sharifs
**********************************************************
____________________________________________________________________________________
Don't get soaked. Take a quick peek at the forecast
with the Yahoo! Search weather shortcut.
http://tools.search.yahoo.com/shortcuts/#loc_weather
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email