java - How to annotate a list using @XmlElement? -
java - How to annotate a list using @XmlElement? -
i have next annotation using javax.xml.bind.annotation.xmlelement
:
@xmlelement public list<string> getkeywords() { homecoming keywords; }
which produces next xml when marshall illustration content:
<keywords>keyword1</keywords> <keywords>keyword2</keywords>
i next xml:
<keywords> <keyword>keyword1</keyword> <keyword>keyword2</keyword> </keywords>
what kind of annotation should use?
i've tried
@xmlelementwrapper @xmlelement(name="keyword")
but whole content disappears , result is:
<keywords/>
the same happens if seek rename element:
@xmlelement(name="keyword")
what doing wrong?
update:
here updated total code class according first answers, still not working (the result empty list <keywords/>
when marshalled xml):
import java.util.list; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlelementwrapper; import javax.xml.bind.annotation.xmlrootelement; @xmlrootelement public class content { private list<string> keywords; public content() {} @xmlelementwrapper(name="keywords") @xmlelement(name="keyword") public list<string> getkeywords() { homecoming keywords; } public void setkeywords(list<string> keywords) { this.keywords = keywords; } }
i tried next same result:
import java.util.list; import javax.xml.bind.annotation.xmlaccesstype; import javax.xml.bind.annotation.xmlaccessortype; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlelementwrapper; import javax.xml.bind.annotation.xmlrootelement; @xmlrootelement @xmlaccessortype(xmlaccesstype.field) public class content { @xmlelementwrapper(name="keywords") @xmlelement(name="keyword") private list<string> keywords; public content() {} public list<string> getkeywords() { homecoming keywords; } public void setkeywords(list<string> keywords) { this.keywords = keywords; } }
however, keywords not empty next produces <keywords>keyword1</keywords><keywords>keyword2</keywords>
instead of empty list:
import java.util.list; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlelementwrapper; import javax.xml.bind.annotation.xmlrootelement; @xmlrootelement public class content { private list<string> keywords; public content() {} @xmlelement public list<string> getkeywords() { homecoming keywords; } public void setkeywords(list<string> keywords) { this.keywords = keywords; } }
the code marshalling (jax-rs):
import java.io.stringwriter; import javax.ws.rs.consumes; import javax.ws.rs.path; import javax.ws.rs.post; import javax.ws.rs.produces; import javax.ws.rs.core.mediatype; import javax.ws.rs.core.response; import javax.xml.bind.jaxbcontext; import javax.xml.bind.jaxbexception; import javax.xml.bind.marshaller; @path("process") @consumes(mediatype.application_json) @produces(mediatype.application_xml) public class contenthandler { @post public response process(content content) { stringwriter stringwriter = new stringwriter(); seek { jaxbcontext jaxbcontext = jaxbcontext.newinstance(content.class); marshaller jaxbmarshaller = jaxbcontext.createmarshaller(); jaxbmarshaller.setproperty(marshaller.jaxb_formatted_output, true); jaxbmarshaller.marshal(content, stringwriter); } grab (jaxbexception e) { homecoming response.servererror().entity(e.getmessage()).build(); } homecoming response.ok(stringwriter.tostring(), mediatype.application_xml).build(); } }
you need leverage @xmlelementwrapper
, @xmlelement
.
content
import java.util.list; import javax.xml.bind.annotation.*; @xmlrootelement public class content { private list<string> keywords; public content() {} @xmlelementwrapper @xmlelement(name="keyword") public list<string> getkeywords() { homecoming keywords; } public void setkeywords(list<string> keywords) { this.keywords = keywords; } }
demo code demo
import java.util.*; import javax.xml.bind.*; public class demo { public static void main(string[] args) throws exception { jaxbcontext jc = jaxbcontext.newinstance(content.class); list<string> strings = new arraylist<string>(2); strings.add("foo"); strings.add("bar"); content content = new content(); content.setkeywords(strings); marshaller marshaller = jc.createmarshaller(); marshaller.setproperty(marshaller.jaxb_formatted_output, true); marshaller.marshal(content, system.out); } }
output
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <content> <keywords> <keyword>foo</keyword> <keyword>bar</keyword> </keywords> </content>
for more information below links couple articles blog provide additional information:
http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html http://blog.bdoughan.com/2012/12/jaxb-representing-null-and-empty.html java xml jaxb
Comments
Post a Comment