2012-03-30 43 views
0

我想反序列化下面的XML:簡單的Java,XML反序列化

<jdownloader> 
    <package 
     package_eta="~" 
     package_linksinprogress="0" 
     package_linkstotal="5" 
     package_loaded="0 B" 
     package_name="Unchecked" 
     package_percent="0.00" 
     package_size="0 B" 
     package_speed="0 B" 
     package_todo="0 B" > 

     <file 
      file_hoster="hoster_name" 
      file_name="name" 
      file_package="Unchecked" 
      file_percent="0.09" 
      file_speed="0" 
      file_status="[Aborted] " > 

      <file 
       file_hoster="hoster_name" 
       file_name="name" 
       file_package="Unchecked" 
       file_percent="0.03" 
       file_speed="0" 
       file_status="[Aborted] " > 

       <file 
        file_hoster="hoster_name" 
        file_name="name" 
        file_package="Unchecked" 
        file_percent="0.05" 
        file_speed="0" 
        file_status="[Aborted] " > 
       </file> 
      </file> 
     </file> 
    </package> 
</jdownloader> 

我似乎無法得到簡單的XML反序列化這就是我想要的。

我需要獲取一個包(每個xml中將包含多個包)的對象以及作爲包對象的子對象的文件對象列表。 XML無法更改,並且看起來每個文件元素都嵌套在前一個文件元素下,而不是全部位於父級包下。

我有這個迄今爲止的代碼是:

@Root(name = "jdownloader") 
public class DownloadsModel { 

    @ElementList(name="package") 
    public List<PackageModel> Package; 

} 


@Root(name = "package") 
public class PackageModel { 

    @ElementList(name="file") 
    public List<FileModel> file; 

    @Attribute 
    public String package_eta; 

    @Attribute 
    public String package_linksinprogress; 

    @Attribute 
    public String package_linkstotal; 

    @Attribute 
    public String package_loaded; 

    @Attribute 
    public String package_name; 

    @Attribute 
    public String package_percent; 

    @Attribute 
    public String package_size; 

    @Attribute 
    public String package_speed; 

    @Attribute 
    public String package_todo; 

} 

@Root(name = "file") 
public class FileModel { 

    @Attribute 
    public String file_hoster; 

    @Attribute 
    public String file_name; 

    @Attribute 
    public String file_package; 

    @Attribute 
    public String file_percent; 

    @Attribute 
    public String file_speed; 

    @Attribute 
    public String file_status; 

} 

當簡單的XML試圖反序列化的XML,它尋找的包類文件元素的屬性。

有人可以幫我嗎?

回答

0

要開始了,請嘗試更換:

public class PackageModel { 

     @ElementList(name="file") 
     public List<FileModel> file; 

與此

public class PackageModel { 

     @ElementList(name="file", inline=true) 
     public List<FileModel> file; 

然後你可以離開你的多重FileModel定義。

更多詳情here

0

你的模型就像這就是

@Root(name = "package") 
public class DownloadsModel { 

    @ElementList(name = "package", inline = true) 
    public List<PackageModel> Package; 
} 

套餐:

@Root(name = "package") 
public class PackageModel { 

    @ElementList(name = "file", inline = true) 
    public List<FileModel> file; 
    .... 
} 

和文件(該XML不能改變,而且似乎每個文件元素以前的文件元素嵌套,而不是所有的包裝父母)

@Root(name = "file") 
public class FileModel { 

@ElementList(name = "file", inline = true, required = false) 
public List<FileModel> file; 

@Attribute 
public String file_hoster; 

.... 
}