If i declare the namespace on the root element, like this:
@JacksonXmlRootElement(namespace = "urn:stackify:jacksonxml", localName = "PersonData")
public class Person {
private String id;
private String name;
private String note;
}
It produces:
<PersonData xmlns="urn:stackify:jacksonxml">
<id xmlns="">12345</id>
<name xmlns="">Graham</name>
<note xmlns="">Hello</note>
</PersonData>
But I want the namespace only on the root element. The xmlns attribute should not appear on child elements.
How can i archive this?
Annotation Type JacksonXmlRootElementAnnotation that can be used to define name of root element used for the root-level object when serialized, which normally uses name of the type (class). It is similar to JAXB XmlRootElement .
We can also read XML, using the various readValue APIs that are part of provided by the ObjectMapper. For example, reading some XML from an InputStream into a Java Bean: MyBean bean = objectMapper.
dataformat. xml. annotation. Package that contains additional annotations that can be used to configure XML-specific aspects of serialization and deserialization.
There is a workaround which I found more elegant for me.
You may define constant for your namespace like this:
@JacksonXmlRootElement(localName = "PersonData")
public class Person {
@JacksonXmlProperty(isAttribute = true)
private final String xmlns = "urn:stackify:jacksonxml";
private String id;
private String name;
private String note;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With