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

发布于 2024-07-16 07:54:01 字数 3528 浏览 7 评论 0原文

我有这样的架构:

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋日私语 2024-07-23 07:54:01

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

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?

幻梦 2024-07-23 07:54:01

我不知道这是否是您的确切问题,但您可能不需要 而是 。 也就是说,您的架构应该是:

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

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">
  <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文