java - 使用 xstream 生成子类从 xml 解码

标签 java xstream

我正在尝试使用 XStream 将 XML 转换为对象树。我想根据属性创建一个特定的子类。

我该如何去做呢?

<items>
    <item>
        <event type="aaa">
            <timestamp>2014-04-10 15:58:08 UTC</timestamp>
        </event>
        <event type="bbb">
            <timestamp>2014-04-03 11:58:08 UTC</timestamp>
        </event>
    </item>
</items>

当我简单地将 XStream 与别名和 ONE Event 类一起使用时,它工作得很好。

    xstream.alias("items", Items.class);
    xstream.alias("event", Event.class);

但是,我希望 XStream 为每个事件类型创建一个不同的类。 我有类 EventAAA 和 EventBBB,它们都是从抽象事件扩展而来的。我如何告诉 XStream 在解码时考虑到这一点? XStream 目前总是尝试实例化 Event 并失败,因为它是抽象的。

干杯!

最佳答案

执行您想要的操作(以及更多)的示例代码:

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * http://stackoverflow.com/posts/23792750
 */
public class App {

    public static class Items {

        private ArrayList<Item> e;

        public Items(ArrayList<Item> e) {
            this.e = e;
        }

    }

    public static class Item {

        private ArrayList<Event> e;

        public Item(ArrayList<Event> e) {
            this.e = e;
        }

    }

    public static void main(String[] args) {
        Items items = new Items(
                new ArrayList<Item>(
                        Arrays.asList(
                                new Item(
                                        new ArrayList(
                                                Arrays.<Event>asList(new EventAAA(), new EventBBB())
                                        )
                                )
                        )
                )
        );

        XStream xs = new XStream();

        xs.registerConverter(new EventConverter());
        xs.registerConverter(new ItemConverter());
        xs.alias("item", Item.class);
        xs.addImplicitArray(Item.class, "e");
        xs.alias("items", Items.class);
        xs.addImplicitArray(Items.class, "e");

        System.out.println("Serialize individual event objects:");
        System.out.println(xs.toXML(new EventAAA()));
        System.out.println(xs.toXML(new EventBBB()));
        System.out.println("De-serialize individual event objects:");
        System.out.println(xs.fromXML(xs.toXML(new EventAAA())).toString());
        System.out.println(xs.fromXML(xs.toXML(new EventAAA())).getClass().getName());
        System.out.println(xs.fromXML(xs.toXML(new EventBBB())).toString());
        System.out.println(xs.fromXML(xs.toXML(new EventBBB())).getClass().getName());

        System.out.println("Show serialization of ArrayList<Item> items:");
        System.out.println(xs.toXML(items));

        System.out.println("Show de-serialization of ArrayList<Item> items:");
        System.out.println(xs.fromXML(xs.toXML(items)));

        System.out.println("Show correct type information in de-serialization for elements in e:");
        Items items2 = (Items) xs.fromXML(xs.toXML(items));
        for (Item i : items2.e) {
            for (Event e : i.e) {
                System.out.println(e.getClass().getName());
            }
        }
    }

    public static class Timestamp {

        public Timestamp(String timestamp) {

        }
    }

    public static abstract class Event {

        public abstract String getTypeName();

        private Timestamp timestamp = new Timestamp("");

        public void setTimestamp(Timestamp t) {
            this.timestamp = t;
        }

        public Timestamp getTimestamp() {
            return timestamp;
        }
    }

    public static class EventAAA extends Event {

        @Override
        public String getTypeName() {
            return "aaa";
        }

    }

    public static class EventBBB extends Event {

        @Override
        public String getTypeName() {
            return "bbb";
        }

    }

    public static class ItemConverter implements Converter {

        @Override
        public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext mc) {
            Item i = (Item) o;
            for (Event e : i.e) {
                writer.startNode("event");
                mc.convertAnother(e);
                writer.endNode();
            }
        }

        @Override
        public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext uc) {
            Item i = new Item(new ArrayList<>());
            while (reader.hasMoreChildren()) {
                i.e.add((Event) uc.convertAnother(i, Event.class));
            }
            return i;
        }

        @Override
        public boolean canConvert(Class type) {
            return (type.equals(Item.class));
        }

    }

    public static class EventConverter implements Converter {

        public boolean canConvert(Class clazz) {
            return Event.class.isAssignableFrom(clazz);
        }

        public void marshal(Object value, HierarchicalStreamWriter writer,
                MarshallingContext context) {
            Event e = (Event) value;
            writer.addAttribute("type", e.getTypeName());
            writer.startNode("timestamp");
            writer.setValue(e.getTimestamp().toString());
            writer.endNode();
        }

        public Object unmarshal(HierarchicalStreamReader reader,
                UnmarshallingContext context) {
            String type = reader.getAttribute("type");
            Event e;
            if (type.equals("aaa")) {
                e = new EventAAA();
            } else if (type.equals("bbb")) {
                e = new EventBBB();
            } else {
                throw new IllegalArgumentException("Encountered illegal type of event: " + type);
            }
            reader.moveDown();
            e.setTimestamp(new Timestamp(reader.getValue()));
            reader.moveUp();
            return e;
        }

    }

}

示例代码的输出:

Serialize individual event objects:
<App_-EventAAA type="aaa">
  <timestamp>App$Timestamp@184cf7cf</timestamp>
</App_-EventAAA>
<App_-EventBBB type="bbb">
  <timestamp>App$Timestamp@5bfa9431</timestamp>
</App_-EventBBB>
De-serialize individual event objects:
App$EventAAA@48fa0f47
App$EventAAA
App$EventBBB@161479c6
App$EventBBB
Show serialization of ArrayList<Item> items:
<items>
  <item>
    <event type="aaa">
      <timestamp>App$Timestamp@5c909414</timestamp>
    </event>
    <event type="bbb">
      <timestamp>App$Timestamp@65466a6a</timestamp>
    </event>
  </item>
</items>
Show de-serialization of ArrayList<Item> items:
App$Items@3eb7fc54
Show correct type information in de-serialization for elements in e:
App$EventAAA
App$EventBBB

如您所见,EventAAAEventBBB被连载到<event>具有适当 type 的节点属性,但是 <event>反序列化回正确的对象类型( App$EventAAAApp$EventBBB ,最后两行输出,“App$”前缀来自内部类的使用)。

您应该能够在您的代码中几乎按原样使用它;您需要将 stub 类替换为您的真实类。

关于java - 使用 xstream 生成子类从 xml 解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23675218/

相关文章:

java - 具有相同方法的多个接口(interface)最终由类实现

java - 如何访问转换器内的 XStream 对象

java - 如何忽略 XStream 的类型?

java - 为什么在这个例子中我们不能使用图形对象绘制字符串

java - android需要比较onSensorChanged方法的当前和之前的event.values

java - 我应该读什么书来替代 "Head First Servlets and JSP"?

java - 家庭作业问题。为日历约会程序编写方法时遇到问题

java - com.thoughtworks.xstream.mapper.CannotResolveClassException

java - 第二次解析 XML 文件时出现 XStream ConversionException

java - 优化 xstream 加载速度