KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Perhaps a stupid question: I have a List of type <Data> which I want to marshal into a XML file. This is my class Database containing an ArrayList ... @XmlRootElement public class Database { List<Data> records = new ArrayList<Data>(); public List<Data> getRecords() { return records; } public void setRecords(List<Data> records) { this.records = records; } } ...and this is class Data: // @XmlRootElement public class Data { String name; String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } Using the following test class... public class Test { public static void main(String args[]) throws Exception { Data data1 = new Data(); data1.setName("Peter"); data1.setAddress("Cologne"); Data data2 = new Data(); data2.setName("Mary"); data2.setAddress("Hamburg"); Database database = new Database(); database.getRecords().add(data1); database.getRecords().add(data2); JAXBContext context = JAXBContext.newInstance(Database.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(database, new FileWriter("test.xml")); } } ...I got the result: <database> <records> <add
Tags (comma-separated)
Save Edits
Cancel