我有一个带有@XmlRootElement(name="objectName",namespace="https:blahblah")
的类,并且该类中的某些属性全部带有@XmlElement(namespace="https:blahblah")
但是现在我有了一些没有XmlElement
注释的元素。为什么还要编组?
我只想编组带注释的属性。
代码如下:
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是例外配置,这意味着它具有将Java对象转换为XML的默认规则。您只需要在需要覆盖默认规则的地方提供元数据,这样可以大大减少必须完成的配置量。
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/TheBasics
您可以指定@XmlAccessorType(XmlAccessType.NONE)
,以便仅对带注释的字段/属性进行编组。
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Root {
@XmlElement
private String foo; // Will be marshalled
private String bar; // Will not be marshalled
}
了解更多信息
http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html