tags:

views:

639

answers:

2

I have this schema:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <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);
    }

}
A: 

Without you showing your Java class, I'm guessing you do not have a setter method setCuit(String) but only have an attribute declared as public String _cuit?

Arjan
Hi. The setter is there... I have edited the question to add the class.
Germán
And you're not using any specific Castor mapping file, right? Does Castor actually recognize <consulta> within <mensaje> then?I wonder which of the two lines is actually throwing the exception: Unmarshaller.unmarshal or con.validate.
Arjan
No mapping file. I assume it recognizes <consulta> because it complains about its child... the line that throws MarshalException is the unmarshal() method.
Germán
A: 

I don't know if this is your exact problem or not, but you probably do not want <xs:all> but instead <xs:sequence>. That is, your schema should be:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:element block="" final="" name="mensaje">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="1" maxOccurs="1" name="identificacion" type="Head" />
        <xs:element minOccurs="1" maxOccurs="1" name="consulta">
          <xs:complexType>
            <xs:sequence>
              <xs:element minOccurs="1" maxOccurs="1" name="cuit" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="Head">
    <!-- etc etc -->
  </xs:complexType>
</xs:schema>
Eddie
If I understand right, the only difference between 'sequence' and 'all' is that the former enforces child order. Anyway I tried your suggestion but I get the same thing...
Germán
In that case, I'll try this later today and respond back with what I see.
Eddie
For me, using Castor 1.3, your code runs and validates, so there is some non-obvious problem. What version of Castor are you using? Try 1.3.
Eddie
Thanks for your time, I'll check on Monday... but out of frustration I already did away with Castor in that project!
Germán