gpt4 book ai didi

java - XStream: UnknownFieldException - 没有这样的字段

转载 作者:行者123 更新时间:2023-11-29 03:21:42 26 4
gpt4 key购买 nike

当我尝试将 xml 反序列化为 java 对象时,出现以下异常。

com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException:没有这样的字段 xmlserializer.EntityFields.EntityFields。

我在以下链接中看到了您的解决方案,但它对我不起作用: UnknownFieldException - No such field


package xmlserializer;

public class EntityFields {

private List<EntityField> entityFields = new ArrayList<EntityField>();
public List<EntityField> getEntityFields() {
return entityFields;
}
public void setEntityFields(List<EntityField> entityFields) {
this.entityFields = entityFields;
}
}

package xmlserializer;

public class EntityField {

private String FieldName;
private String FieldDisplayName;
private String FieldType;
private boolean IsMultivalued = false;

public String getFieldName() {
return FieldName;
}
public void setFieldName(String fieldName) {
FieldName = fieldName;
}
public String getFieldDisplayName() {
return FieldDisplayName;
}
public void setFieldDisplayName(String fieldDisplayName) {
FieldDisplayName = fieldDisplayName;
}
public String getFieldType() {
return FieldType;
}
public void setFieldType(String fieldType) {
FieldType = fieldType;
}
public boolean getIsMultivalued() {
return IsMultivalued;
}
public void setIsMultivalued(boolean isMultivalued) {
IsMultivalued = isMultivalued;
}
}

主类:

    xstream.alias("EntityField", EntityField.class);        
xstream.alias("EntityFields", EntityFields.class);
EntityFields entityFields = (EntityFields)xstream.fromXML(buff.toString());

xml 内容:

                             <EntityFields> 
<EntityField>
<FieldName>Question 1</FieldName>
<FieldDisplayName>some question 1</FieldDisplayName>
<FieldType>character varying</FieldType>
<IsMultivalued>false</IsMultivalued>
</EntityField>
<EntityField>
<FieldName>Question 2</FieldName>
<FieldDisplayName>some question 2</FieldDisplayName>
<FieldType>boolean</FieldType>
<IsMultivalued>false</IsMultivalued>
</EntityField>
<EntityField>
<FieldName>Question 3</FieldName>
<FieldDisplayName>some question 3</FieldDisplayName>
<FieldType>character varying[]</FieldType>
<IsMultivalued>true</IsMultivalued>
</EntityField>
</EntityFields>

请帮忙。

谢谢

最佳答案

    The following solution worked for me:

XStream xstream = new XStream(new DomDriver());

xstream.alias("EntityFields", EntityFields.class);
xstream.addImplicitCollection(EntityFields.class, "entityFlds");
xstream.alias("EntityField", EntityField.class);

EntityFields entityFields = (EntityFields)xstream.fromXML(buff.toString());

关于java - XStream: UnknownFieldException - 没有这样的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218745/

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