我正在使用Simple框架在我的Android应用程序上反序列化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;
}
}
如果我将mapper类更改为:
@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的大小是正确的,但是如何以不同的方式获取每个对象,包括扩展名(属性)
请帮我解决这个问题。
由于
答案 0 :(得分:1)
@ElementList(name="config")
public List<ConfigObject> configObjects = new ArrayList<ConfigObject>();
这与您上面列出的xml不匹配。虽然基于@Element
的解决方案(示例1)将创建正确的config
- 标记,但该列表会添加另一个“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>
解决方案:使用内联列表 - 那些没有“list-tag”。
@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;
// ...
}
答案 1 :(得分: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...
}