我应该如何在Java中使用简单的xml映射抽象类?

时间:2010-06-08 17:33:40

标签: java simple-framework

我想使用简单的xml框架(http://simple.sourceforge.net/)来实现以下xml:

<events>
    <course-added date="01/01/2010">
        ...
    </course-added>
    <course-removed date="01/02/2010">
        ....
    </course-removed>
    <student-enrolled date="01/02/2010">
        ...
    </student-enrolled>
</events>

我有以下内容(但它没有达到所需的xml):

@Root(name="events")
class XMLEvents {

    @ElementList(inline=true)
    ArrayList<XMLEvent> events = Lists.newArrayList();

        ...
}


abstract class XMLEvent {

    @Attribute(name="date")
    String dateOfEventFormatted;

    ...

}

不同类型的XMLNode具有不同的信息(但都是不同类型的事件)

@Root(name="course-added")
class XMLCourseAdded extends XMLEvent{

    @Element(name="course") 
    XMLCourseLongFormat course;

    ....
}

@Root(name="course-removed")
class XMLCourseRemoved extends XMLEvent {

    @Element(name="course-id")
    String courseId;

        ...

}

我应该如何进行映射或者我应该更改什么才能实现所需的xml?

谢谢!

2 个答案:

答案 0 :(得分:0)

解决问题的一种非常简洁的方法是创建自己的转换器,例如:

public class XMLEventsConverter implements Converter<XMLEvents> {

    private Serializer serializer;

    XMLEventsConverter(Serializer serializer){
        this.serializer = serializer;
    }

    @Override
    public XMLEvents read(InputNode arg0) throws Exception {
        return null;
    }

    @Override
    public void write(OutputNode node, XMLEvents xmlEvents) throws Exception {
        for (XMLEvent event : xmlEvents.events) {
            serializer.write(event, node);
        }
    }

}

然后使用RegistryStrategy并将类XMLEvents与前一个转换器绑定:

private final Registry registry = new Registry();
private final Serializer serializer = new Persister(new RegistryStrategy(registry));
....
registry.bind(XMLEvents.class, new XMLEventsConverter(serializer));

以这种方式获得的xml是所需的。请注意,XMLEventsConverter上的read方法只是return null,如果您需要从xml文件重建对象,则应该正确实现它。

问候!

答案 1 :(得分:0)

看起来您现在可以使用@ElementListUnion功能

执行此操作
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.ElementListUnion;
import org.simpleframework.xml.core.Persister;

import java.io.StringReader;
import java.util.List;

/**
 * Created by dan on 3/09/16.
 */
public class XMLEventsTest {

  public static final String XML_EVENTS = "<?xml version=\"1.0\" " +
      "encoding=\"ISO-8859-1\"?><events>" +
      "<course-added date=\"2016/10/1\"/>" +
      "<course-removed date=\"2016/10/2\"/>" +
      "</events>";

  public static void main(String args[]) throws Exception {
    Persister persister = new Persister();
    XMLEvents events = persister.read(XMLEvents.class, new StringReader(XML_EVENTS));

    for (XMLEvent e : events.events) {
      System.out.println("found event: " + e);
    }
  }

  public static class XMLEvents {
    @ElementListUnion({
        @ElementList(entry = "course-added", type = XMLCourseAdded.class, inline = true),
        @ElementList(entry = "course-removed", type = XMLCourseRemoved.class, inline = true),
    })
    private List<XMLEvent> events;
  }

  public static class XMLEvent {
    @Attribute(name = "date")
    private String dateOfEventFormatted;

    public String getDateOfEventFormatted() {
      return dateOfEventFormatted;
    }

    @Override
    public String toString() {
      return getClass().getSimpleName() + "[" + dateOfEventFormatted + "]";
    }
  }

  public static class XMLCourseAdded extends XMLEvent {
  }

  public static class XMLCourseRemoved extends XMLEvent {
  }


}

这将打印出来:

found event: XMLCourseAdded[2016/10/1]
found event: XMLCourseRemoved[2016/10/2]