I have this schema:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element block="" final="" name="mensaje">
<xs:complexType>
<xs:all>
<xs:element minOccurs="1" maxOccurs="1" name="identificacion" type="Head" />
<xs:element minOccurs="1" maxOccurs="1" name="consulta">
<xs:complexType>
<xs:all>
<xs:element minOccurs="1" maxOccurs="1" name="cuit" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
<xs:complexType name="Head">
<!-- etc etc -->
</xs:complexType>
</xs:schema>
As you can see, the element "cuit" is required. I have already created the classes that map to the schemas with Castor. But I'm trying to test the unmarshalling, this way:
public void testCastorUnmarshalling()
{
String xml = new StringBuffer("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
.append("<mensaje>")
.append(" <identificacion>")
// etc
.append(" </identificacion>")
.append(" <consulta>")
.append(" <cuit>35890</cuit>")
.append(" </consulta>")
.append("</mensaje>")
.toString();
StringReader xmlReader = new StringReader(xml);
Consulta con = null;
try {
con = (Consulta) Unmarshaller.unmarshal(Consulta.class, xmlReader);
con.validate();
} catch (ValidationException ex) {
fail("Validacion: " + ex.getMessage());
} catch (MarshalException ex) {
fail("Exception: " + ex.getMessage());
}
}
The cuit field is there but I get:
Exception: The field '_cuit' (whose xml name is 'cuit') is a required field of class 'XML.entities.Consulta'
Any ideas why?
Here's the generated class for Consulta (comments removed...)
package XML.entities;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
public class Consulta implements java.io.Serializable {
private java.lang.String _cuit;
public Consulta() {
super();
}
public java.lang.String getCuit(
) {
return this._cuit;
}
public boolean isValid(
) {
try {
validate();
} catch (org.exolab.castor.xml.ValidationException vex) {
return false;
}
return true;
}
public void marshal(
final java.io.Writer out)
throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
Marshaller.marshal(this, out);
}
public void marshal(
final org.xml.sax.ContentHandler handler)
throws java.io.IOException, org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
Marshaller.marshal(this, handler);
}
public void setCuit(
final java.lang.String cuit) {
this._cuit = cuit;
}
public static XML.entities.Consulta unmarshal(
final java.io.Reader reader)
throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
return (XML.entities.Consulta) Unmarshaller.unmarshal(XML.entities.Consulta.class, reader);
}
public void validate(
)
throws org.exolab.castor.xml.ValidationException {
org.exolab.castor.xml.Validator validator = new org.exolab.castor.xml.Validator();
validator.validate(this);
}
}