• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XML Validation in Castor

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we validate XML file against XSD in Castor.

Please help me out.

Thanks.
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case you are using the code generation approach

I guess CASTOR by default validates the Object model.
What it means is that it generates some validation code based on the xsd and when we try to load data into the objects it validates the data against the validation code generated. By default the validation by XML parser is switched off to avoid the overhead.You can enable it by modifying the castor.properties file



However the xsd support given by castor is not complete so if you are using code generation then CASTOR may not be the best choice. try XMLBeans or JAXB.

In case you are not using Code generation then you have to validate your XML against your XSD by using some parser and then pass it to castor only if it is validated successfully.

Hope it helps.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.

I am not generating code from Castor. I am writing bean classes and mapping file myself.

I got your points to validate XML before it goes to Castor through some parser. What can be the simplest, efficient and easy way to validate XML against XSD. Please help me...

Thanks.
[ August 01, 2005: Message edited by: rathi ji ]
 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Since you are going to only validate your document I suggest you use the SAX parser. It will be faster. To know how to enable validation read this chapter http://www.cafeconleche.org/books/xmljava/chapters/ch07s04.html#SAXValidator.java

Regards
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Firstly, Thanks to Rajagopal.

But the above method I made for XML validation against XSD through SAX is not working. I am confuse, what is going wrong. It is not going into catch but giving this *error*.



Please help me...

Thanks a lot.
[ July 29, 2005: Message edited by: rathi ji ]
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing, whether XML is valid or invalid, the error message is same.

Thanks.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Coming to my original problem, how can I validate my XML file against XSD through castor. If I am not genarating code from castor (I am writing bean classes and mapping file myself).

Why I can't use JDOM/DOM/SAX for validation, because XML file will be loaded twice into memory. First at the time of validation through some parser and second at the time of parsing through castor.

I guess, it is serious (at least not recommended).

I am sure that must be someting in castor, otherwise no body will use castor for XML parsing...

Please guide me, what should I do?

Thanks a lot.
[ August 04, 2005: Message edited by: rathi ji ]
 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

There is a property in the castor.properties file called
org.exolab.castor.validation make it true.

coming to your validation question looks like the parser did not find the location of youe schema or dtd. pass the file representing the dtd or xsd file as a feature "http://java.sun.com/xml/jaxp/properties/schemaSource"
 
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic