Marshal an attribute as a type of list like nested root element using JAXB

I want to save a list of hospitals using JAXB API, every hospital has own list of services. For doing that i had implemented the following classes

public class Wrapper {

private List items = new ArrayList();

@XmlAnyElement(lax=true)

public List getItems() {

return items;

}

}

/*************************************/

@XmlAccessorType(XmlAccessType.FIELD)

@XmlRootElement(name="hopital")

public class Hopital {

private int id;

private String nom,adresse,categorie="";

@XmlElement(name="service")

private List services=new ArrayList<>();

private static List liste=new ArrayList<>();

static File springDir;

static JAXBContext context;

static BufferedWriter writer = null;

//constructeurs

//getters and setters

public static void addHopital(Hopital ... hops) throws Exception{

File hfile = new File("hopitaux.xml");

Wrapper hopitaux = new Wrapper();

for (int i = 0; i < hops.length; i++) {

hopitaux.getItems().add(hops[i]);

}

writer...

Read More »

By: StackOverFlow - Monday, 16 April

Related Posts