我正在尝试使用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为每个Event类型创建一个不同的类。
我有EventAAA和EventBBB类,它们均从抽象Event扩展。在解组时如何告诉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 $”前缀来自内部类的使用)。

您应该可以按原样使用此代码;您将需要用实际类替换存根类。

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

10-11 22:16