XStream 使用属性字段作为另一个字段的 XML 标记的属性,将 class 序列化为 XML

XStream Serialize a class to XML using an attribute field as attribute for XML tag of another field

我有这个class

public class EventXML{
    private String name;
    private String ip;
    private Date date;
    private String dateFormat;
    private String eventName;
}

使用此功能:

public String toXML(){
        String x;
        XStream xs = new XStream();
        x = xs.toXML(this);
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+x;
    }

我得到这个结果:

<?xml version="1.0" encoding="UTF-8"?>
    <EventXML>
      <name>SuperFarmer</name>
      <ip>IPPPPPPP</ip>
      <myData>2018-05-15 12:48:05.343 UTC</myData>
      <dateFormat>HH:mm:ss dd/MM/yyyy</dateFormat>
      <eventName>CLICCA</eventName>
    </EventXML>

但我想要这样的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<EventXML>
  <name>SuperFarmer</name>
  <ip>IPPPPPPP</ip>
  <myData dateFormat="HH:mm:ss dd/MM/yyyy">12:48:05 15/05/2018</myData>
  <eventName>CLICCA</eventName>
</EventXML>

你能给我一些提示来获得我的 objective 吗?

通过对 EventXML class 进行一些更改,我能够达到所需的 xml 输出。我创建了一个包含 <myData> 元素和属性的内部 class:

public class EventXML
{
    public String name;
    public String ip;
    public MyData myData = new MyData();
    public String eventName;

    public static class MyData {
        public Date date;
        public String dateFormat;
    }
}

现在在序列化方法中,我们告诉 xstream 如何将 MyData 序列化为 xml 元素,并告诉 xstream 如何转换日期字段:

  1. ToAttributedValueConverter 接受 class(第一个参数)和字段名称(最后一个参数),命名字段将用作元素的值,而所有其他字段class 将用作属性

  2. DateConverter 被 xstream 用来转换 Date 类型的字段,我给它 dateFormat 作为参数。

这是它的样子:

public String toXML() {
    String x;
    XStream xs = new XStream();

    // the following tell XStream how to craete myData element:
    // the last arg is the field to be used as element value and all other fields are attributes
    xs.registerConverter(new ToAttributedValueConverter(MyData.class, xs.getMapper(), xs.getReflectionProvider(),
        xs.getConverterLookup(), "date"));
    // register date converter according to dateFormat field
    xs.registerConverter(new DateConverter(myData.dateFormat, new String[]{}));

    x = xs.toXML(this);
    return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+x;
}

如果使用Xstream 1.14.x,使用注释。

EventXML 对象应如下所示:

public class EventXML {
    public String name;
    public String ip;

    @XStreamAlias("myData")
    public MyData myData = new MyData();

    public String eventName;

    @XStreamConverter(value=ToAttributedValueConverter.class, strings={"date"})
    public static class MyData {

        public Date date;

        @XStreamAsAttribute
        public String dateFormat;
    }
}

Maven 依赖项(如果您使用 Maven):

<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.10</version>
</dependency>

您的 toXml 方法应该如下所示(使用 processAnnotations):

public String toXml() {
    XStream xstream = new XStream();
    // Note the processAnnotations:
    xstream.processAnnotations(EventXML.class);
    String x = xstream.toXML(this);
    return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + x;
}