在jaxb中生成列表标记和对象标记

时间:2013-04-23 10:39:33

标签: java xml tags jaxb

我想在jaxb中生成列表标记和对象标记。 我有两节课。

@XmlRootElement(name = "firstclass")
    class Firstclass{
     List<secondclass> secondclassList = new ArrayList<secondclass>();

     //constructors, getters, setters

      @XmlElement(name = "secondclassList")
       public void setsecondclassList(List<Secondclass> secondclassList) {
       this.secondclassList= secondclassList;
      }

    }

第二类代码

@XmlRootElement(name = "secondclass")
    class Secondclass{
     String name;
     String attribute;

     //constructors, getters, setters
    }

xml转换后,我得到像

这样的输出
<firstclass>
    <secondclassList>
      <name>my name</name>
      <attribute>attr value 11</attribute>
    <secondclassList>
    </secondclassList>
      <name>my name</name>
      <attribute>attr value 22</attribute>
    </secondclassList>
</firstclass>

但我想要像这样输出

<firstclass>
    <secondclassList>
         <secondclass>
              <name>my name</name>
              <attribute>attr value 11</attribute>
         </secondclass>
         <secondclass>
              <name>my name</name>
              <attribute>attr value 22</attribute>
         </secondclass>
     </secondclassList>
</firstclass>

1 个答案:

答案 0 :(得分:4)

尝试使用:

  @XmlElementWrapper(name = "secondclassList")
  @XmlElement(name="secondclass")
  public void setsecondclassList(List<Secondclass> secondclassList) {