Reputation: 9103
I'm using jaxb to make xml file and store it in my pc, and i want when making another file to store it in addition to the first file in the same xml file that is on my pc, for example this is the first file:
<File>
<thing1>
</thing1>
</file>
And i want the second file to be like that :
<File>
<thing1>
</thing1>
<thin2g>
</thing2>
</file>
And that's the Java code i use for marshaling:
public ConfList(Object obj){
this.obj = obj;
}
public void addToLXML() throws IOException, JAXBException {
File file = new File(fileName);
JAXBContext jaxbContext = JAXBContext.newInstance(Xml.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(obj, file);
jaxbMarshaller.marshal(obj, System.out);
}
Upvotes: 0
Views: 220
Reputation: 376
If I understand your question correctly, you want to be able to add new elements to an existing XML file? So the first time you create the file, the "thing1" element would be added, and the second time the program is run, the "thing2" element would be added?
Assuming this is the case, the best way to do this would be to unmarshal the existing file into an object that contains an annotated list of "thing" elements (assuming they are the same type or share a common parent class). Then simply add your new "thing" to the list, and marshal it back to file.
If this didn't address your question, please clarify what you are looking for, and I'll see what I can do to help!
Steve
Upvotes: 1