Reputation: 383
I am using javax.xml.bind.annotation.XmlRootElement annotated object to serialize it into xml string.
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
// Marshal the object to a StringWriter
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.example.com/schema.xsd");
StringWriter stringWriter = new StringWriter();
marshaller.marshal(obj, stringWriter);
result = stringWriter.toString();
How to exclude some fields of the object in order not to send them? What annotation that class field has to be annotated in order to exclude it from final string.
Upvotes: 0
Views: 4024
Reputation: 72854
Use the @XmlTransient annotation:
Prevents the mapping of a JavaBean property/type to XML representation.
@XmlTransient
public String toBeSkippedField;
Upvotes: 2
Reputation: 1707
You could use the transient
keyword to omit a field from being serialized.
For example:
int k;
transient int j;
Variable j
will not be serialized as we have mentioned the transient
keyword.
Upvotes: 1