2014-11-03 90 views
1

我正在使用簡單框架反序列化我的android應用程序上的xml。使用簡單框架解析XML的正確方法

問題出在xml的一部分上,因爲我可以獲取其他部分的對象。

這裏是我掙扎的XML的一部分:

<webtv name="channelname" date="2014-10-31"> 
     <config> 
       <pathVideo extension="mp4">http://vodflash.channelname.com/tv_conn/1/country/</pathVideo> 
       <pathBigImage extension="jpg">http://vodflash.channelname.com/tv_conn/1/country/pic/320x180/</pathBigImage> 
       <pathImage extension="jpg">http://vodflash.channelname.com/tv_conn/1/country/pic/160x90/</pathImage> 
       <pays>GB;IE</pays> 
      </config> 

這裏是我XmlMapper類:

@Root(name="webtv", strict = false) 
public class XmlModelMapper { 

    public ConfigObject getConfigObjects() { 
     return configObject; 
    } 

    @Element(name="config") 
    public ConfigObject configObject = new ConfigObject(); 


    @ElementList(name = "itemList") 
    private List<Videos> itemList = new ArrayList<Videos>(); 
    public List<Videos> getItemList() { 
     return itemList; 
    } 

    @ElementList(name = "chaineList") 
    private List<Chaine> chaineList = new ArrayList<Chaine>(); 
    public List<Chaine> getChaineList() { 
     return chaineList; 
    } 
} 

如果我改變我的映射類這樣的:

@Root(name="webtv", strict = false) 
public class XmlModelMapper { 

    public List<ConfigObject> getConfigObjects() { 
     return configObject; 
    } 

    //change is here using a list not an object 
    @ElementList(name="config") 
    public List<ConfigObject> configObjects = new ArrayList<ConfigObject>(); 


    @ElementList(name = "itemList") 
    private List<Videos> itemList = new ArrayList<Videos>(); 
    public List<Videos> getItemList() { 
     return itemList; 
    } 

    @ElementList(name = "chaineList") 
    private List<Chaine> chaineList = new ArrayList<Chaine>(); 
    public List<Chaine> getChaineList() { 
     return chaineList; 
    } 
} 

和日誌大小的List我得到4這是正確的,但如何以不同的方式獲得每個對象,包括t他的擴展(屬性)

請幫我解決這個問題。

感謝

+1

您應該突出版本之間的差異:-D使用一個* DIFF *實際看到它。 – ollo 2014-11-05 19:55:16

回答

1
@ElementList(name="config") 
public List<ConfigObject> configObjects = new ArrayList<ConfigObject>(); 

這不會匹配上面列出你的XML。基於@Element的解決方案(示例1)將創建一個合適的config-tag,該列表將添加另一個「list-tag」;這裏有一個例子:

<config> <!-- This is the list's tag, wrapping all elements --> 
    <config> <!-- This is the actual element's tag --> 
     <pathVideo extension="mp4">http://video.com</pathVideo> 
     <pathBigImage extension="jpg">http://bigimg.com</pathBigImage> 
     <pathImage extension="jpg">http://image.com</pathImage> 
     <pays>GB;IE</pays> 
    </config> 

    <!-- some more <config>...</config> --> 
</config> 

解決辦法:用athe 直列名單 - 那些不具備「列表標籤」。

@ElementList(name = "config", inline = true, entry = "config") 
public List<ConfigObject> configObjects = new ArrayList<>(); 

爲了完整起見,這裏使用我的班來進行測試:

class Chaine { /* empty - enough for testing */ } 

class Videos { /* empty - enough for testing */ } 


@Root 
public class ConfigObject 
{ 
    @Element(name = "pathVideo") 
    private PathConfig video; 
    @Element(name = "pathBigImage") 
    private PathConfig bigImage; 
    @Element(name = "pathImage") 
    private PathConfig image; 
    @Element(name = "pays") 
    private String pays; 

    // ... 
} 


@Root 
public class PathConfig 
{ 
    @Attribute(name = "extension") 
    private String extension; 
    @Text 
    private String path; 

    // ... 
} 
+0

謝謝oLLo的回答,我昨天設法解決了這個問題,我會在下面發佈我的代碼,但我認爲你的更好,因此將其標記爲正確答案。 – moujib 2014-11-05 23:33:26

1

解決了我的問題,很多瞭解簡單的框架,這是偉大的,重量輕,但我更喜歡OLLO的答案 請丟棄ormlite註釋 我的課:

@Root(name="webtv", strict = false) 
public class XmlModelMapper { 

    public ConfigObject getConfigObject() { 
     return configObject; 
    } 

    @Element(name="config") 
    public ConfigObject configObject = new ConfigObject(); 


    //rest of the elements 
    ... 


} 

@Root(name = "config" , strict = false) 
public class ConfigObject { 


    @Element 
    PathVideo pathVideo; 

    @Element 
    PathImage pathImage; 

    @Element 
    PathBigImage pathBigImage ; 


    //rest of the elements 
} 

@Root(name = "pathImage") 
@DatabaseTable(tableName = "imageconfig") 
public class PathImage { 

    @DatabaseField(generatedId = true) 
    Integer id ; 


    @Attribute 
    @DatabaseField 
    String extension; 

    @Text 
    @DatabaseField 
    String pathImage; 


    //rest of the elements... 
} 

@Root(name = "pathVideo") 
@DatabaseTable(tableName = "videoconfig") 
public class PathVideo { 

    @DatabaseField(generatedId = true) 
    Integer id ; 

    @Attribute 
    @DatabaseField 
    String extension; 

    @Text 
    @DatabaseField 
    String pathVideo; 


} 

最後

@Root(name = "pathBigImage") 
@DatabaseTable(tableName = "bigimageconfig") 
public class PathBigImage { 


    @DatabaseField(generatedId = true) 
    Integer id ; 

    @Attribute 
    @DatabaseField 
    String extension; 

    @Text 
    @DatabaseField 
    String pathBigImage; 

    //rest of the elements... 
}