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

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

问题描述

我正在尝试编组实现通用接口的对象列表.涉及 3 个类和 1 个接口:

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

Community 类(有一个方法:List getPeople();)

Person 接口(有一个方法:String getName();)

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

Girl 类(实现 Person)

Girl class (implements Person)

Boy 类(实现 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>

我意识到我可以将元素更改为其他内容,但我希望元素名称是在 Girl 或 Boy 类中指定的名称.

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:

以下是您的示例的外观:

Here is how it would look for your example:

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

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

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