如何通过 XStream 读取具有属性的列表元素

How to read list elements with attribute via XStream(如何通过 XStream 读取具有属性的列表元素)
本文介绍了如何通过 XStream 读取具有属性的列表元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 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 读取具有属性的列表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)