Java and JAXB: trying to write a list of mixed objects to xml -


i'm trying write list of different type of objects xml using jaxb, without luck. possible using jaxb ? need properties (not properties defined interface), of objects, written xml file.

in main class :

    list<itransport> mylist = new arraylist<>();      bike b = new bike();     b.setspeed(1);     b.setcolor("yellow");     mylist.add(b);      car c = new car();     c.setdoors(5);     c.setspeed(10);     mylist.add(c);      train t = new train();     t.setlength(10);     t.setspeed(50);     mylist.add(t);      transport tr = new transport();     tr.setvehicles(mylist);      // save data     try {         jaxbcontext context = jaxbcontext.newinstance(itransport.class);         marshaller marshaller = context.createmarshaller();         marshaller.setproperty(marshaller.jaxb_formatted_output, true);          // write file         string homedir = system.getproperty("user.home");         string filepath = homedir + file.separator + "test.xml";         file f = new file(filepath);         marshaller.marshal(tr, f);      } catch (jaxbexception ex) {     } 

my interface class:

    @xmlrootelement     @xmlaccessortype(xmlaccesstype.field)     public interface itransport {          public int getspeed();         public void setspeed(int _speed);      } 

my collection class:

    @xmlrootelement(name="vehicles")     @xmlaccessortype(xmlaccesstype.field)     public class transport {          @xmlelement(name="transport", type=itransport.class)         list<itransport> vehicles = new arraylist<>();          public list<itransport> getvehicles() {             return vehicles;         }          public void setvehicles(list<itransport> vehicles) {             this.vehicles = vehicles;         }      } 

my bike class:

    @xmlrootelement     @xmlaccessortype(xmlaccesstype.field)     public class bike implements itransport{          private int speed = 0;         private string color = "";          @override         public int getspeed() {             return speed;         }          @override         public void setspeed(int _speed) {             speed = _speed;         }          public string getcolor() {             return color;         }          public void setcolor(string color) {             this.color = color;         }      } 

the car , train classes ar same bike class, other properties.

does have idea on how marshall/unmarshall list of mixed objects ?

thanks feedback


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -