java - JAXB marshall elements with comments -


so have class

@xmlaccessortype(value = xmlaccesstype.field) public class item {  @xmlattribute private string ida;  private string namea;  @xmlattribute private string idb;  private string nameb;  @xmlattribute private string idc;  private string namec;  public item() { }  public item(string ida, string namea, string idb, string nameb, string idc, string namec) {     this.ida = ida;     this.namea = namea;     this.idb = idb;     this.nameb = nameb;     this.idc = idc;     this.namec = namec; }  @override public string tostring() {     return "item [namea = " + namea + ", nameb = " + nameb + ", namec = " + namec + "]"; } } 

and container:

@xmlrootelement @xmlaccessortype(xmlaccesstype.field) public class items {  private list<item> items;  public items() { }  public items(list<item> items) {     this.items = items; } } 

and i'm using listener write comments after each item:

public class addcommentslistener extends marshaller.listener {  private xmlstreamwriter xsw;  public addcommentslistener(xmlstreamwriter xsw) {     this.xsw = xsw; }  @override public void beforemarshal(object source) { }  @override public void aftermarshal(object source) {     if (source instanceof item) {         try {             xsw.writecomment(source.tostring());         } catch (xmlstreamexception e) {         }     }  } } 

finally test class is:

public class test { public static <t> void savetoxml(t entity, outputstream stream) {     try {         jaxbcontext jc = jaxbcontext.newinstance(entity.getclass());         marshaller marshaller = jc.createmarshaller();         marshaller.setproperty(marshaller.jaxb_formatted_output, true);         marshaller.setlistener(new addcommentslistener(xmloutputfactory.newfactory().createxmlstreamwriter(stream)));         marshaller.marshal(entity, stream);     } catch (jaxbexception | xmlstreamexception e) {         system.out.println(e);     } }  public static void main(string[] args) throws filenotfoundexception {     list<item> items = new arraylist<>();     (int =0 ;i < 100; i++) {         string ida = string.format("ida %s", i);         string namea = string.format("locationa %s", i);         string idb = string.format("idb %s", i);         string nameb = string.format("locationb %s", i);         string idc = string.format("idc %s", i);         string namec = string.format("locationc %s", i);         items.add(new item(ida, namea, idb, nameb, idc, namec));     }     path path = paths.get("out.txt");     savetoxml(new items(items), new fileoutputstream(path.tofile())); } } 

and output:

<items ida="ida 17" idb="idb 17" idc="idc 17">     <namea>locationa 17</namea>     <nameb>locationb 17</nameb>     <namec>locationc 17<!--item [namea = locationa 17, nameb = locationb 17, namec = locationc 17]--><!--item [namea = locationa 18, nameb = locationb 18, namec = locationc 18]--><!--item [namea = locationa 19, nameb = locationb 19, namec = locationc 19]--><!--item [namea = locationa 20, nameb = locationb 20, namec = locationc 20]--><!--item [namea = locationa 21, nameb = locationb 21, namec = locationc 21]--><!--item [namea = locationa 22, nameb = locationb 22, namec = locationc 22]--></namec> </items> 

but comment inside of element, combined, , sometimes, ideas how have comments after every item, , of course after element?


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -