Reputation: 2338
Suppose i have a xml file which has several nodes & children. I am using jaxb (unmarshalling & marshalling) to update the xml file when requires but wanted to know what exactly happens when..... ??
<parent>
<node>abc</node>
</parent>
now i wanted to update this xml by adding <node>xyz</node>
, so what i do
Unmaeshall this xml file to java Object and add this new node as java object.
Marshall the updated Object to XML file.
my Question is : what happens when we marshall the java object to xml file ?
option a) xml file remove everything and write afresh.
option b) xml file only updated by just adding the new line.
Upvotes: 1
Views: 936
Reputation: 148977
If you are strictly talking about File
objects then the answer given by Bozho is correct. If you consider the DOM representation then JAXB offers both approaches:
Unmarshaller/Marshaller
In the following code originalDOM != marshalledDOM.
Node originalDOM; // Populate original document
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Customer customer = (Customer) unmarshaller.unmarshal(orginalDocument);
Marshaller marshaller = jc.createMarshaller();
marshalledDOM = marshaller.getNode(customer);
Binder
When using a Binder
a link is maintained between the objects and the DOM nodes they were unmarshalled from. If you modify the unmarshalled object the Binder
alows you to apply those changes back to the original DOM. This approach is very useful when there is unmapped content in the document that you need to keep (such as comments and processing instructions).
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Binder<Node> binder = jc.createBinder();
Customer customer = (Customer) binder.unmarshal(document);
customer.getAddress().setStreet("2 NEW STREET");
binder.updateXML(customer);
For More Information
Upvotes: 1
Reputation: 597046
By default the contents are overridden.
Only if you use m.marshal(jaxbObj, new FileOutputStream(file, true))
(append=true), then the new content will be appended.
Upvotes: 2