File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes XML Validation in Castor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML Validation in Castor" Watch "XML Validation in Castor" New topic
Author

XML Validation in Castor

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Can we validate XML file against XSD in Castor.

Please help me out.

Thanks.
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Nov 26, 2004
Posts: 183
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

Joined: Oct 11, 2004
Posts: 3830



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

Joined: Oct 11, 2004
Posts: 3830
One more thing, whether XML is valid or invalid, the error message is same.

Thanks.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Nov 26, 2004
Posts: 183
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"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML Validation in Castor
 
Similar Threads
XML to Java object using JAXB
XML / Castor Object
Castor version compatibility
compare two XML files using DOM parser
Favorite java-xml mapping tool ?