java - Serialize Object to XML with Jackson -
i want convert object xml. using com.fasterxml.jackson.dataformat.xml.xmlmapper serialize object xml
i used javax.xml.bind.annotation.* annotate class , variables.
@xmlrootelement @xmlaccessortype(xmlaccesstype.field) public class transaction { @xmlattribute(required = true, name = "ch-full-name") private string fullname; @xmlattribute(required = true, name = "ch-address") private string address; @xmlattribute(required = true, name = "ch-city") private string city; @xmlattribute(required = true, name = "ch-zip") private string zipcode; @xmlattribute(required = true, name = "ch-country") private string country; @xmlattribute(required = true, name = "ch-phone") private string phone; @xmlattribute(required = true, name = "ch-email") private string email; ...getters; & setters; } assigning values , serializing:
transaction tran = new transaction(); tran.setfullname("full name"); tran.setaddress("address"); tran.setemail("email"); tran.setcity("city"); tran.setcountry("country"); tran.setzipcode("zip"); tran.setphone("phone"); xmlmapper mapper = new xmlmapper(); mapper.enable(serializationfeature.indent_output); string xml = "<?xml version='1.0' encoding='utf-8'?>" + mapper.writevalueasstring(tran); so xml returns this:
<?xml version="1.0" encoding="utf-8"?> <transaction> <fullname>full name</fullname> <address>address</address> <city>city</city> <zipcode>zip</zipcode> <country>country</country> <phone>phone</phone> <email>email</email> </transaction> but supposed this:
<?xml version="1.0" encoding="utf-8"?> <transaction> <ch-full-name>full name</ch-full-name> <ch-address>address</ch-address> <ch-city>city</ch-city> <ch-zip>zip</ch-zip> <ch-country>country</ch-country> <ch-phone>phone</ch-phone> <ch-email>email</ch-email> </transaction> is there correct way set attribute names xml? how can change names including class name(transaction name should transaction)?
since using jackson, need use @jsonproperty("name") in order give different names variables while serializing. part of jackson.
@jsonproperty("ch-full-name") private string fullname; @jsonproperty("ch-address") private string address; @jsonproperty("ch-city") private string city; @jsonproperty("ch-zip") private string zipcode; @jsonproperty("ch-country") private string country; @jsonproperty("ch-phone") private string phone; @jsonproperty("ch-email") private string email;
Comments
Post a Comment