To serialize an XML key in Java, you can use the JAXB (Java Architecture for XML Binding) API. JAXB provides a way to convert Java objects into XML and vice versa.
To serialize an XML key using JAXB, follow these steps:
@XmlRootElement(name="key")
public class Key {
@XmlElement(name="value")
private String value;
public Key() {}
public Key(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
JAXBContext context = JAXBContext.newInstance(Key.class);
Key key = new Key("my_key");
Marshaller marshaller = context.createMarshaller();
StringWriter writer = new StringWriter();
marshaller.marshal(key, writer);
String xmlString = writer.toString();
The resulting XML string would be:
<key>
<value>my_key</value>
</key>
Asked: 2023-07-12 02:32:38 +0000
Seen: 9 times
Last updated: Jul 12 '23