java - JAXB : XmlElementWrapper nested nodes -
i want generate xml :
<mainnode> <node1></node1> <node2></node2> </mainnode> <mainnode2></mainnode2>
and how generate mainnode1 , mainnode2 , node1 in code:
@xmlelementwrapper(name = "mainnode") @xmlelement(name = "node1") public list<string> getvalue() { return value; } @xmlelement(name = "mainnode2") public string getvalue2() { return value2; }
how add node2 mainnode1 ?
you don't seem have root element in example. obtain structure 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 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());
... , here's printout:-
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <node> <mainnode> <node1>node1 value</node1> <node2>node2 value</node2> </mainnode> <mainnode2/> </node>
Comments
Post a Comment