Castor解组异常要求提供的字段

时间:2009-03-31 20:21:04

标签: java castor

我有这个架构:

<?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>

如您所见,元素“cuit”是必需的。我已经创建了使用Castor映射到模式的类。但我正试图以这种方式测试解组:

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());
     }
  }

cuit字段在那里,但我得到了:

异常:字段'_cuit'(其xml名称为'cuit')是类'XML.entities.Consulta'的必填字段

任何想法为什么?

这是Consulta的生成类(删除了注释...)

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);
    }

}

2 个答案:

答案 0 :(得分:0)

如果没有你展示你的Java类,我猜你没有setter方法setCuit(String)但只有一个属性声明为public String _cuit

答案 1 :(得分:0)

我不知道这是否是您的确切问题,但您可能不希望<xs:all>而是<xs:sequence>。也就是说,您的架构应该是:

<?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: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>