java - JAXB Marshaller does not want elements whos value is null or empty -


i not want empty xml balise when use jaxb.

pojo code:

@xmlrootelement(name = "ligne") public class ligne {      private string ridecount;      @xmlelement(name = "nb_trajet")     public string getridecount() {         return ridecount;     }      public void setridecount(string ridecount) {         this.ridecount = ridecount;     } } 

jaxb code:

public string jaxbmoreportsobjecttoxml(moreports moreports) {     string xmlstring = "";     try {         jaxbcontext context = jaxbcontext.newinstance(moreports.class);         marshaller m = context.createmarshaller();         m.setproperty(marshaller.jaxb_formatted_output, boolean.true);         stringwriter sw = new stringwriter();         m.marshal(moreports, sw);         xmlstring = sw.tostring();      } catch (jaxbexception e) {         throw new technicalexception(e.getmessage());      }      return xmlstring; } 

package-info code:

@xmljavatypeadapters({ @xmljavatypeadapter(value = stringadapter.class, type = string.class) }) package com.rivasi.business.aipenet.bean;  import javax.xml.bind.annotation.adapters.xmljavatypeadapter; import javax.xml.bind.annotation.adapters.xmljavatypeadapters; 

stringadapter code:

import javax.xml.bind.annotation.adapters.xmladapter;  public class stringadapter extends xmladapter<string, string> {      @override     public string unmarshal(string v) throws exception {         return v;     }      @override     public string marshal(string v) throws exception {         if ("".equals(v)) {             return null;         }         return v;     }  } 

i find solution for:

<lignes>     <ligne>         <nb_trajet/>     </ligne>     <ligne>         <nb_trajet>zone3</nb_trajet>     </ligne>     <ligne>         <nb_trajet/>     </ligne>     <ligne>         <nb_trajet/>     </ligne>     <ligne>         <nb_trajet/>     </ligne> </lignes> 

or

<lignes>     <ligne>         <nb_trajet></nb_trajet>     </ligne>     <ligne>         <nb_trajet>zone3</nb_trajet>     </ligne>     <ligne>         <nb_trajet></nb_trajet>     </ligne>     <ligne>         <nb_trajet></nb_trajet>     </ligne>     <ligne>         <nb_trajet></nb_trajet>     </ligne> </lignes> 

but want:

<lignes>     <ligne>         <nb_trajet>zone3</nb_trajet>     </ligne> </lignes> 


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -