gpt4 book ai didi

java - Castor 解码异常要求提供的字段

转载 作者:行者123 更新时间:2023-11-29 09:26:25 30 4
gpt4 key购买 nike

我有这个架构:

<?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 字段在那里,但我得到:

异常(exception):字段“_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);
}

}

最佳答案

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

关于java - Castor 解码异常要求提供的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/702865/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com