1

我想在 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>
4

1 回答 1

4

尝试使用:

  @XmlElementWrapper(name = "secondclassList")
  @XmlElement(name="secondclass")
  public void setsecondclassList(List<Secondclass> secondclassList) {
于 2013-04-23T10:54:49.323 回答