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);
}
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你没有显示你的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 aspublic String _cuit
?我不知道这是否是您的确切问题,但您可能不需要
而是
。 也就是说,您的架构应该是: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: