I want to serialize several instances of a class to an XML file using JAXB. One class is easy to serialize, but several are already a problem. Show how it can be done.

I used the code that I found here: JAXB-Demo .

Here is something like this:

ArrayList<Campaign> cam = new ArrayList<Campaign>(); cam.add(camp); // добавляем объект класс Campaign cam.add(camp2); // добавляем объект класс Campaign JAXBContext jaxbContext = JAXBContext.newInstance(Campaign.class); Marshaller marsh = jaxbContext.createMarshaller(); marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marsh.marshal(cam,new FileOutputStream("ololo.xml")); 
  • Well serialize the collection of the elements you need - Mage
  • one
    What do you want to suggest every step? so many monotonous questions about serialization. Or at least update the old question. - rasmisha

1 answer 1

JAXB supports list serialization. Therefore, it is sufficient to create a list with the objects to be serialized and serialize it.

Another approach is to use XStream instead of JAXB. It serializes complex object hierarchies.

  • Something does not work, can the code show how it will look? - Stas0n
  • Where can I read about the serialization of lists in JAXB? - Stas0n