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

发布于 2024-09-04 23:45:59 字数 1160 浏览 7 评论 0原文

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

<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;

        ...

}

我应该如何进行映射或者我应该更改什么才能成为能够实现de想要的xml吗?

谢谢!

I want to achieve the following xml using simple xml framework (http://simple.sourceforge.net/):

<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>

I have the following (but it doesn't achieve the desired xml):

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

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

        ...
}


abstract class XMLEvent {

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

    ...

}

And different type of XMLNodes that have different information (but are all different types of events)

@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;

        ...

}

How should I do the mapping or what should I change in order to be able to achieve de desired xml?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑,眼淚并存 2024-09-11 23:45:59

解决该问题的一种非常干净的方法是创建您自己的转换器,例如:

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 方法只是返回 null,如果您需要从 xml 文件重建对象,您应该正确实现它。

问候!

A very clean way to solve the problem is to create your own converter such as:

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);
        }
    }

}

And then use a RegistryStrategy and bind the class XMLEvents with the previous converter:

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

In this way the xml obtained is the one desired. Note that the read method on the XMLEventsConverter just return null, in case you need to rebuild the object from the xml file you should properly implement it.

Regards!

何时共饮酒 2024-09-11 23:45:59

看起来您现在可以使用 @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]

Looks like you can do this now using the @ElementListUnion functionality

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 {
  }


}

This will print out:

found event: XMLCourseAdded[2016/10/1]
found event: XMLCourseRemoved[2016/10/2]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文