java JAXB:XmlElementWrapper 嵌套节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5099170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:26:09  来源:igfitidea点击:

JAXB : XmlElementWrapper nested nodes

javajaxb

提问by Jimmy

I want to Generate XML that look like this :

我想生成如下所示的 XML:

    <mainNode>   
       <node1></node1> 
       <node2></node2> 
    </mainNode>
   <mainNode2></mainNode2> 

and this is how i generate the mainNode1 , mainNode2 and node1 in my code:

这就是我在代码中生成 mainNode1 、 mainNode2 和 node1 的方式:

   @XmlElementWrapper(name = "mainNode")
        @XmlElement(name = "node1")
        public List<String> getValue() {
            return value;
        }

   @XmlElement(name = "mainNode2")
   public String getValue2() {
   return value2;
   }

how i could add node2 to the mainNode1 ?

我如何将 node2 添加到 mainNode1 ?

采纳答案by limc

You don't seem to have a root element in your example. You could do something like this to obtain the structure you want:-

您的示例中似乎没有根元素。你可以做这样的事情来获得你想要的结构:-

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
class Node {
    private MainNode    mainNode;
    private MainNode2   mainNode2;

    public Node() {
    }

    public Node(MainNode mainNode, MainNode2 mainNode2) {
        this.mainNode = mainNode;
        this.mainNode2 = mainNode2;
    }

}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
class MainNode {
    private String  node1;
    private String  node2;

    public MainNode() {
    }

    public MainNode(String node1, String node2) {
        this.node1 = node1;
        this.node2 = node2;
    }

}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
class MainNode2 {

}

Here's my test code:-

这是我的测试代码:-

JAXBContext jc = JAXBContext.newInstance(Node.class);
Marshaller m = jc.createMarshaller();

MainNode mainNode = new MainNode("node1 value", "node2 value");
MainNode2 mainNode2 = new MainNode2();
Node node = new Node(mainNode, mainNode2);

StringWriter sw = new StringWriter();

m.marshal(node, sw);

System.out.println(sw.toString());

... and here's the printout:-

...这是打印输出:-

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<node>
  <mainNode> 
    <node1>node1 value</node1>
    <node2>node2 value</node2>
  </mainNode>
  <mainNode2/>
</node>

回答by Kanagavelu Sugumar

XmlElementWrapper should be used only when the wrapperElement has a list of same type of elements.

仅当 wrapperElement 具有相同类型元素的列表时,才应使用 XmlElementWrapper。

<node> 
   <idList>
       <id> value-of-item </id>
       <id> value-of-item </id>
       ....
   </idList>
</node>

 @XmlAccessorType(XmlAccessType.FIELD)
 @XmlRootElement
 class Node {
    @XmlElementWrapper(name = "idList")
    @XmlElement(name = "id", type = String.class)
    private List<String> ids = new ArrayList<String>;
 //GETTERS/SETTERS
 }