<PlayListSettings>
<PlayList>
<id>PlayList1</Name>
<File class="string">/mnt/sdcard/Video1</File>
</PlayList>
</PlayListSettings>
这是我的Serialized xml,其中File是字符串的arrayList。我可以使用xstream.aliasSystemAttribute(null,“class”)删除class =“string”;
and the output is
<PlayListSettings>
<PlayList>
<id>PlayList1</Name>
<File>/mnt/sdcard/Video1</File>
</PlayList>
</PlayListSettings>
如何使用XStream进行反序列化?
答案 0 :(得分:1)
您可以使用@XStreamImplicit
注释。这是一个完整的例子:
@XStreamAlias("PlayList")
public final class PlayList {
@XStreamImplicit(itemFieldName = "File")
private final List<String> files = new ArrayList<>();
public void addFile(final String file) {
checkFile(file);
files.add(file);
}
public void removeFile(final String file) {
checkFile(file);
files.remove(file);
}
public List<String> getFiles() {
return Collections.unmodifiableList(files);
}
private void checkFile(final String file) {
if (file == null) {
throw new NullPointerException();
} else if (file.isEmpty()) {
throw new IllegalArgumentException("is empty");
}
}
public static void main(String[] args) {
final PlayList playList = new PlayList();
playList.addFile("Foo");
playList.addFile("Bar");
// Serialize
final XStream xstream = new XStream();
xstream.autodetectAnnotations(true);
final String xml = xstream.toXML(playList);
System.out.println(xml);
// Deserialize
final PlayList playList2 = (PlayList) xstream.fromXML(xml);
for (final String file : playList.getFiles()) {
System.out.println(file);
}
}
}
输出如下:
<PlayList>
<File>Foo</File>
<File>Bar</File>
</PlayList>