我正在使用 XStream 读取下面的示例 xml 文件.
I'm using XStream to read below example xml file.
<list>
<file>/setup/x86-linux2/bin/zip.txt</file>
<file type="dir">/src/bin/</file>
<name>test xml</name>
</list>
下面是我阅读上面xml的代码,
Below is my code for reading above xml,
public class ListWithConverter {
public static class FileConvertor implements Converter {
public boolean canConvert(final Class clazz) {
return clazz.equals(MyFile.class);
}
public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) {
throw new UnsupportedOperationException("Not supported to write file element yet."); //$NON-NLS-1$
}
public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) {
MyFile file = new MyFile();
for (Iterator<String> iter = reader.getAttributeNames(); iter.hasNext(); ){
String name = iter.next();
if (name.equals("type")) //$NON-NLS-1$
file.type = reader.getAttribute(name);
}
file.path = reader.getValue();
return file;
}
}
@XStreamAlias("list")
public class MyList {
@XStreamAlias("name")
String name;
@XStreamImplicit(itemFieldName="file") @XStreamConverter(FileConvertor.class)
List<MyFile> files;
}
public static class MyFile {
String type;
String path;
}
public static void main(String[] args) throws MalformedURLException, IOException {
XStream xstream = new XStream();
xstream.setClassLoader(MyList.class.getClassLoader());
xstream.processAnnotations(MyList.class);
InputStream stream = new File("test.xml").toURL().openStream();
MyList list = (MyList)xstream.fromXML(stream);
System.out.println(list.name);
for (MyFile f : list.files) {
System.out.println(f.path);
}
}
}
我的程序的输出是,
test xml
null
null
看起来 XStream 不支持同时使用注解@XStreamImplicit"和@XStreamConverter".
Looks like XStream does not support using annotation '@XStreamImplicit' and '@XStreamConverter' at the same time.
我的问题是如何通过 XStream 读取示例 xml?
My question is how should I do to read the example xml via XStream?
我在迁移到XStream 1.4.x后找到了解决方案,
I found a solution after migrating to XStream 1.4.x,
@XStreamAlias("list")
public class MyList {
@XStreamAlias("name")
String name;
@XStreamImplicit(itemFieldName="file")
List<MyFile> files;
}
@XStreamAlias("file")
@XStreamConverter(value=ToAttributedValueConverter.class, strings={"path"})
public static class MyFile {
@XStreamAlias("type")
@XStreamAsAttribute
String type;
String path;
}
这篇关于如何通过 XStream 读取具有属性的列表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
上传进度侦听器未触发(Google 驱动器 API)Upload progress listener not fired (Google drive API)(上传进度侦听器未触发(Google 驱动器 API))
使用 Google Drive SDK 将文件保存在特定文件夹中Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 将文件保存在特定文件夹中)
Google Drive Android API - 无效的 DriveId 和 Null ResourcGoogle Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 无效的 DriveId 和 Null ResourceId)
谷歌驱动api服务账户查看上传文件到谷歌驱动使Google drive api services account view uploaded files to google drive using java(谷歌驱动api服务账户查看上传文件到谷歌驱动使用java
Google Drive 服务帐号返回 403 usageLimitsGoogle Drive service account returns 403 usageLimits(Google Drive 服务帐号返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory;Google Drcom.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例