I have generated from the following .xsd
<=====start
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:simpleType name="LiteralDescription">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="40"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="Stuff" type="LiteralDescription"/>
<xsd:complexType name="OuterElType">
<xsd:sequence minOccurs="0" >
<xsd:sequence >
<xsd:element ref="Stuff"/>
</xsd:sequence>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="OuterEl" type="OuterElType"/>
</xsd:schema>
==> finish
If I use the generated classes ( generation script is ...
java cp "C:\projects\castor\castor-0.9.4.3\castor
0.9.4.3.jar;C:\projects\castor\castor-0.9.4.3\castor-0.9.4.3-
xml.jar;C:\projects\xerces\xerces-2_0_2\xercesImpl.jar"
org.exolab.castor.builder.SourceGenerator -i Test.xsd -package myTest
... ) to Unmarshall the following file
<==== starts
<OuterElType
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation='Test.xsd'></OuterElType>
===> ends
I get the following exception trace....
Oh no : ValidationException: error-if-this-is-used is a required field.;
- location of error: XPATH: OuterEl{file: [not available]; line: 3; column:
58}
ValidationException: error-if-this-is-used is a required field.;
- location of error: XPATH: OuterEl
at org.exolab.castor.xml.FieldValidator.validate
(FieldValidator.java:195)
at org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate
(XMLClassDescriptorImpl.java:896)
at org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate
(XMLClassDescriptorImpl.java:764)
at org.exolab.castor.xml.Validator.validate(Validator.java:127)
at org.exolab.castor.xml.UnmarshalHandler.endElement
(UnmarshalHandler.java:695)
at org.apache.xerces.parsers.AbstractSAXParser.endElement
(AbstractSAXParser.java:572)
at org.apache.xerces.impl.XMLNamespaceBinder.endElement
(XMLNamespaceBinder.java:646)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement
(XMLDTDValidator.java:1972)
at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement
(XMLDTDValidator.java:878)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.handleEndElement
(XMLDocumentFragmentScannerImpl.java:1144)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement
(XMLDocumentFragmentScannerImpl.java:987)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.
dispatch(XMLDocumentFragmentScannerImpl.java:1445)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument
(XMLDocumentFragmentScannerImpl.java:333)
at org.apache.xerces.parsers.DTDConfiguration.parse
(DTDConfiguration.java:524)
at org.apache.xerces.parsers.DTDConfiguration.parse
(DTDConfiguration.java:580)
at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:152)
at org.apache.xerces.parsers.AbstractSAXParser.parse
(AbstractSAXParser.java:1169)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:555)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:487)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:627)
at myTest.MainTest.main(MainTest.java:30)
Despite the fact that the XML is legal. I think this comes because
org.exolab.castor.xml.FieldValidator.validate does not check the value of
minOccurs before validating whether any thing as been found.
Also the constructor for the outerEl type descriptor sets the field descriptor
for the outer sequence as required even when ( minOccurs=0 ) it is not.
Okay the example is contrived but the real one we are using is much bigger..
Thanks for Castor it is useful all the same.... Hope this helps...
was the latest release version ( its not available on the drop downs ).
Here is the test program ....
/*
*
*/
package myTest;
import java.io.*;
import org.exolab.castor.xml.*;
/**
*
*/
public class MainTest {
/** Creates a new instance of MainTest */
public MainTest() {
}
/**
*/
public static void main(String[] args) {
try
{ FileReader reader = new FileReader("C:\\projects\\castor\\project- example\\bugzilla\\Test.xml"); OuterEl aMesg = (OuterEl)Unmarshaller.unmarshal(OuterEl.class, reader); System.out.println("Finished " + aMesg); }catch(Exception ex)
{ System.err.println("Oh no : " + ex ); ex.printStackTrace(); }}
}