I have a class with an @XmlRootElement(name="objectName",namespace="https:blahblah") and some attributes in the class all with @XmlElement(namespace="https:blahblah")
But now I have some element without an XmlElement annotation. Why is it also marshalled?
I only want to marshal annotated attributes.
Code looks like this:
JAXBContext jc = JAXBContext.newInstance(SomeClass.class);
Marshaller m = jc.createMarshaller();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().newDocument();
m.marshal(someInstanceOfSomeClass, doc );
JAXB is configuration by exception, this means that it has default rules for converting Java objects to XML. You only need to supply metadata where you need to override the default rules, this greatly reduces the amount of configuration that must be done.
You can specify
@XmlAccessorType(XmlAccessType.NONE)so that only the annotated fields/properties are marshalled.For More Information