JAX-B 正在整理一切
我有一个带有 @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 );
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 );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您不想封送的属性设为
transient
或使用@XmlTransient
对其进行注释。至于确切的原因,我建议您参考规范。Make the attributes you don't want to marshal either
transient
or annotate them with@XmlTransient
. As for the exact why I refer you to the specification.JAXB 是例外配置,这意味着它具有将 Java 对象转换为 XML 的默认规则。您只需在需要覆盖默认规则的地方提供元数据,这大大减少了必须完成的配置量。
您可以指定
@XmlAccessorType(XmlAccessType.NONE)
,以便仅编组带注释的字段/属性。了解更多信息
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