2015-05-17 41 views
2

請告訴我爲什麼會生成異常? 我收到異常,當我嘗試反序列化xml到java對象。XStream。 UnknownFieldException

com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter $ UnknownFieldException:沒有這樣的字段addressservice.Region.region。

---- Debugging information ---- 
field    : region 
class    : addressservice.Region 
required-type  : addressservice.Region 
converter-type  : com.thoughtworks.xstream.converters.reflection.ReflectionConverter 
path    : /AddressServiceConfig/regions/region 
line number   : 3 
class[1]   : addressservice.AddressServiceConfig 
version    : 1.4.7 

我的XML:

<addressServiceConfig> 
    <regions> 
     <region> 
      <regionName>region1</regionName> 
      <provinces> 
       <province> 
        <name>province11</name> 
        <code>11</code> 
       </province> 
       <province> 
        <name>province12</name> 
        <code>12</code> 
       </province> 

      </provinces> 
     </region> 
     <region> 
      <regionName>region2</regionName> 
      <provinces> 
       <province> 
        <name>province21</name> 
        <code>21</code> 
       </province> 
       <province> 
        <name>province22</name> 
        <code>22</code> 
       </province> 
      </provinces> 
     </region> 
    </regions> 

    <pathToCodeposFiles></pathToCodeposFiles> 
</addressServiceConfig> 

我的課表

@XStreamAlias("addressServiceConfig") 
public class AddressServiceConfig 
{ 
    private String pathToCodeposFiles; 
    @XStreamImplicit(itemFieldName = "region") 
    private List<Region> regions; 
} 

@XStreamAlias("region") 
public class Region 
{ 
    private String regionName; 
    @XStreamImplicit(itemFieldName = "province") 
    private List<Province> provinces; 
} 

@XStreamAlias("province") 
public class Province 
{ 
    private int code; 

    private String name; 
} 

請幫助。

回答

1

您的xml定義沒有隱式收藏regionsprovinces但明確的收藏。

您應該從您的類定義中刪除@XStreamImplicit(itemFieldName = "region")@XStreamImplicit(itemFieldName = "province")

明確regions集合:

<addressServiceConfig> 
    <regions> 
    <region> 
    ... 
    </region> 
    <region> 
    ... 
    </region> 
    </regions> 
</addressServiceConfig> 

regions集合:

<addressServiceConfig> 
    <region> 
    ... 
    </region> 
    <region> 
    ... 
    </region> 
</addressServiceConfig>