Ich möchte Object
zu XML
konvertieren. Ich verwende com.fasterxml.jackson.dataformat.xml.XmlMapper
, um das Objekt zu xml zu serialisierenSerialize Objekt zu XML mit Jackson
Ich verwendete javax.xml.bind.annotation.*
, um die Klasse und die Variablen mit Anmerkungen zu versehen.
@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;
}
Werte und Serialisierung zuordnen:
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
kehrt so etwas wie dieses:
<?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>
Aber es soll tatsächlich so sein:
<?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>
Gibt es eine Co? Richtiger Weg, Attributnamen für XML festzulegen? Wie kann ich Namen einschließlich Klassennamen ändern (Transaction
Name sollte transaction
sein)?