使用JaxB编组实现公共接口的对象列表 [英] Marshalling a List of objects implementing a common interface, with JaxB

查看:106
本文介绍了使用JaxB编组实现公共接口的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编组实现公共接口的对象列表。
共有3个类和1个界面:

I am trying to marshall a list of objects implementing a common interface. There are 3 classes and 1 interface involved:

社区类(有一种方法:列表< Person> getPeople ();

界面(有一种方法: String getName();

Person interface (has one method: String getName();)

女孩类(实现人)

男孩 class(实现Person)

Boy class (implements Person)

请参阅下面的代码。

我想要一个如下所示的XML:

I want an XML that looks something like this:

<community>
  <people>
    <girl>
      <name>Jane</name>
    </girl>
    <boy>
      <name>John</name>
    </boy>
    <girl>
      <name>Jane</name>
    </girl>
    <boy>
      <name>John</name>
    </boy>
  </people>
</community>

或可能:

<community>
  <people>
   <person>
      <girl>
        <name>Jane</name>
      </girl>
    </person>
    <person>
      <boy>
        <name>John</name>
      </boy>
    </person>
  </people>
</community>

到目前为止,我得到的是:

So far what I get is this:

<community>
    <people>
        <person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="girl">
            <name>Jane</name>
        </person>
        <person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="boy">
            <name>John</name>
        </person>
        <person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="girl">
            <name>Jane</name>
        </person>
        <person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="boy">
            <name>John</name>
        </person>
    </people>
</community>

我意识到我可以将元素更改为其他内容,但我希望元素名称是名称在女孩或男孩类中加入。

I realize I can change the element to something else, but I want the element name to be the name spesified in the Girl or Boy class.

可以这样做吗?谢谢。

@XmlRootElement(name = "community")
public class Community {

 private List<Person> people;

 @XmlElementWrapper
 @XmlElement(name="person")
 public List<Person> getPeople() {
  return people;
 }

 public Community() {
  people = new ArrayList<Person>();
  people.add(new Girl());
  people.add(new Boy());
  people.add(new Girl());
  people.add(new Boy());
 }
}







@XmlRootElement(name = "girl")
public class Girl implements Person {

 @XmlElement
 public String getName() {
  return "Jane";
 }
}


@XmlRootElement(name = "boy")
public class Boy implements Person {

 @XmlElement
 public String getName() {
  return "John";
 }
}



@XmlJavaTypeAdapter(AnyTypeAdapter.class)
public interface Person {
 public String getName();
}



public class AnyTypeAdapter extends XmlAdapter<Object, Object> {

 @Override
   public Object marshal(Object v) throws Exception {
    return v;
   }

 @Override
   public Object unmarshal(Object v) throws Exception {
    return v;
   }

}


推荐答案

对于这种情况,我建议使用@XmlElements。 @XmlElements用于表示选择的XML模式概念:

For this scenario I would recommend the use of @XmlElements. @XmlElements is used to represent the XML schema concept of choice:

  • http://bdoughan.blogspot.com/2010/10/jaxb-and-xsd-choice-xmlelements.html

以下是您的示例:

@XmlElements({ 
    @XmlElement(name="girl", type=Girl.class),
    @XmlElement(name="boy", type=Boy.class)
})
@XmlElementWrapper
public List<Person> getPeople() {
    return people;
}

@XmlElementRef对应于XML模式中替换组的概念。这就是为什么之前的答案要求将Person从接口更改为类。

@XmlElementRef corresponds to the concept of substitution groups in XML schema. This is why the previous answer requires Person to be changed from an interface to a class.

  • http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-substitution.html

这篇关于使用JaxB编组实现公共接口的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆