XStream:使用属性转换集合 [英] XStream: convert collection with attributes

查看:29
本文介绍了XStream:使用属性转换集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试转换的 XML 如下所示:

The XML I'm trying to convert looks like:

<numberOfEmployees year="2013">499.0</numberOfEmployees>

根据XSD,这些标签可以有多个,所以它是一个集合.生成的代码如下所示:

According to the XSD, there can be multiple of these tags, so it's a collection. The generated code looks like:

    protected List<NumberOfPersonnel> numberOfEmployees;

当我使用 @XStreamImplicit 时,它会丢弃该值,所以我需要一个转换器.但是将 @XStreamImplicit@XStreamConverter 结合起来似乎不起作用.

When I use @XStreamImplicit, it drops the value, so I need a converter. But combining @XStreamImplicit with @XStreamConverter doesn't seem to work.

那我该怎么做呢?我试过弄乱我自己从 CollectionConverter 继承的自定义转换器,但它声称找不到任何孩子,老实说我不知道​​我在做什么.

So how do I do this? I've tried messing about with my own custom converter that inherits from CollectionConverter, but it claims not to find any children, and honestly I have no idea what I'm doing.

有人可以启发我吗?应该不会这么难吧?

Could someone enlighten me? This shouldn't be this hard, should it?

推荐答案

我可以使用 ToAttributedValueConverterNumberOfPersonnel 类和 @XStreamImplicit 在 List-valued 属性上:

I can make it work by using ToAttributedValueConverter on the NumberOfPersonnel class and @XStreamImplicit on the List-valued property:

NumberOfPersonnel.java

import com.thoughtworks.xstream.annotations.*;
import com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter;

// treat the "value" property as the element content and all others as attributes
@XStreamConverter(value = ToAttributedValueConverter.class, strings = {"value"})
public class NumberOfPersonnel {
  public NumberOfPersonnel(int year, double value) {
    this.year = year;
    this.value = value;
  }

  private int year;

  private double value;

  public String toString() {
    return year + ": " + value;
  }
}

Container.java

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.*;
import java.util.List;
import java.util.Arrays;
import java.io.File;

@XStreamAlias("container")
public class Container {
  private String name;

  // any element named numberOfEmployees should go into this list
  @XStreamImplicit(itemFieldName="numberOfEmployees")
  protected List<NumberOfPersonnel> numberOfEmployees;

  public Container(String name, List<NumberOfPersonnel> noEmp) {
    this.name = name;
    this.numberOfEmployees = noEmp;
  }

  public String toString() {
    return name + ", " + numberOfEmployees;
  }

  public static void main(String[] args) throws Exception {
    XStream xs = new XStream();
    xs.processAnnotations(Container.class);

    System.out.println("Unmarshalling:");
    System.out.println(xs.fromXML(new File("in.xml")));

    System.out.println("Marshalling:");
    System.out.println(xs.toXML(new Container("World",
           Arrays.asList(new NumberOfPersonnel(2001, 1000),
                         new NumberOfPersonnel(2002, 500)))));
  }
}

in.xml

<container>
  <name>Hello</name>
  <numberOfEmployees year="2013">499.0</numberOfEmployees>
  <numberOfEmployees year="2012">550.0</numberOfEmployees>
</container>

输出

Unmarshalling:
Hello, [2013: 499.0, 2012: 550.0]
Marshalling:
<container>
  <name>World</name>
  <numberOfEmployees year="2001">1000.0</numberOfEmployees>
  <numberOfEmployees year="2002">500.0</numberOfEmployees>
</container>

这篇关于XStream:使用属性转换集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆