简体   繁体   中英

Setter property in unmarshlling JAXB?

JAXB-> After unmarshalling the XML into Java object, is it possible to set the updated values in the object by using the setter property? Please show some example for setting the value and if we marshall the object back to XML the changes would be get reflected? Please advise

JAXB performs XML to Java conversion (unmarshall) and also Java to XML conversion (marshall).

Try this hello world tutorial to convert a Java object to XML

The example class (with @XmlRootElement)

@XmlRootElement
public class Customer {
    String name;
    int id;

Create the object and set the properties

Customer customer = new Customer();
customer.setId(100);
customer.setName("mkyong");

Marshal the Customer object to a file in XML format

JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(customer, file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM