简体   繁体   English

Jaxb解组,包装了每个元素的列表

[英]Jaxb unmarshalling, list of every element is wrapped

This xml have a wrapper of whole list: 该xml具有整个列表的包装器:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <listWrapper>
    <box>
      <number>1</number>
    </box>
    <box>
      <number>2</number>
    </box>
    <box>
      <number>3</number>
    </box>
  </listWrapper>
</root>

and we can read like this: 我们可以这样阅读:

@XmlElementWrapper(name = "listWrapper")
@XmlElement(name = "box")
List<Box> boxList;

and describe Box class with "box" as root element 并用“ box”作为根元素描述Box类

But how i can make the same list if i have wrapped each box instead all of? 但是,如果我包装了每个盒子而不是所有盒子,我该如何列出相同的清单?

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <wrapper>
    <box>
      <number>1</number>
    </box>
  </wrapper>
  <wrapper>
    <box>
      <number>2</number>
    </box>
  </wrapper>
  <wrapper>
    <box>
      <number>3</number>
    </box>
  </wrapper>
</root>

In the end i want to list of box without wrapper level. 最后,我想列出没有包装级别的包装盒。

The root class (main method to test mapping): 根类(测试映射的主要方法):

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Root {


    private List<Wrapper> wrappers = new ArrayList<Wrapper>();  

    public Root(){

    }

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Root root = new Root();

        Wrapper wrapper1 = new Wrapper(new Box(1));
        root.getWrappers().add(wrapper1);

        Wrapper wrapper2 = new Wrapper(new Box(2));
        root.getWrappers().add(wrapper2);

        Wrapper wrapper3 = new Wrapper(new Box(3));
        root.getWrappers().add(wrapper3);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }


    @XmlElement(name = "wrapper")
    public List<Wrapper> getWrappers() {
        return wrappers;
    } 
    public void setWrappers(List<Wrapper> wrappers) {
        this.wrappers = wrappers;
    }

}

Wrapper class which contains Box: 包装盒类,其中包含Box:

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Wrapper {

    private Box box;

    public Wrapper(){

    }

    public Wrapper(Box box){
        this.box = box;
    }   

    public Box getBox() {
        return box;
    }
    public void setBox(Box box) {
        this.box = box;
    }

}

Finally, Box class: 最后,Box类:

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Box {

    private Integer number;

    public Box(){

    }

    public Box(Integer number){
        this.number = number;
    }   

    public Integer getNumber() {
        return number;
    }
    public void setNumber(Integer number) {
        this.number = number;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM