简体   繁体   English

将内联元素列表直接反序列化为List

[英]Deserialize inline elementlist directly into a List

I have the following XML structure: 我有以下XML结构:

<keys>
   <key>
      <tag>
         someValue
      </tag>
   </key>
   <key>
      <tag>
         someValue
      </tag>
   </key>
</keys>

The key element is represented in code in the following class: key元素在以下类的代码中表示:

public class Key {

    @Element(name = "tag")
    private String tag;
}

My goal is to deserialize these directly into a List , something like this: 我的目标是将这些直接反序列化为List ,如下所示:

Serializer serializer = new Persister();
List<Key> list = serializer.read(Key.class, inputStream); // pseudo code

How can i achieve this with Simple ? 我如何用Simple实现这一目标?

Use the @ElementList annotation like so 像这样使用@ElementList注释

@Root(name="keys")
public class KeyList {

    @ElementList(inline=true, entry="key")
    private List<Key> keys;

    public List<Key> getKeys() {
       return keys;
    }
}

Then 然后

Persister persister = new Persister();
List<Key> keys = persister.read(KeyList.class, source).getKeys();

I would recommend using JAXB (Java Architecture for XML Binding) 我建议使用JAXB(Java Architecture for XML Binding)

Here's a link to a couple of tutorials: 这里有几个教程的链接:

They can explain it better than me. 他们可以比我更好地解释它。 But basically you use annotations defined in the javax.xml.bind.annotation package which define your xml structure. 但基本上你使用javax.xml.bind.annotation包中定义的注释来定义你的xml结构。 Then create a simple JAXB Handler class to handle marshaling and unmarshaling. 然后创建一个简单的JAXB Handler类来处理编组和解组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM