2014-04-22 75 views
1

當我嘗試將xml反序列化爲java對象時,出現以下異常。XStream:UnknownFieldException - 沒有這樣的字段

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> 

請幫助。

謝謝

回答

3
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()); 
相關問題