用JAXB将元素表示为布尔值? [英] Representing element as boolean with JAXB?

查看:68
本文介绍了用JAXB将元素表示为布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有以下XML:

  <Summary>
     <ValueA>xxx</ValueA>
     <ValueB/>
  </Summary>

<ValueB/>永远不会有任何属性或内部元素.这是一个布尔型元素-存在(true)或不存在(false).

<ValueB/> will never have any attributes or inner elements. It's a boolean type element - it exists (true) or it doesn't (false).

JAXB生成了一个具有String valueA成员的Summary类,这很好.但是对于ValueB,JAXB生成了ValueB内部类和相应的成员:

JAXB generated a Summary class with a String valueA member, which is good. But for ValueB, JAXB generated a ValueB inner class and a corresponding member:

@XmlElement(name = "ValueB")
protected Summary.ValueB valueB;

但是我想要的是一个boolean成员和 no 内部类:

But what I'd like is a boolean member and no inner class:

@XmlElement(name = "ValueB")
protected boolean valueB;

您该怎么做?

我不想重新生成类,我只是想手动更改代码.

I'm not looking to regenerate the classes, I'd like to just make the code change manually.

更新:根据接受的答案,我们创建了一个新方法,该方法以valueB == null为条件返回布尔值.

Update: In line with the accepted answer, we created a new method returning the boolean value conditional on whether valueB == null.

当我们使用Hibernate时,我们用@Transient注释了valueB,并用Hibernate的@Column注释对了布尔型吸气剂.

As we are using Hibernate, we annotated valueB with @Transient and annotated the boolean getter with Hibernate's @Column annotation.

推荐答案

jaxb创建一个内部类很合乎逻辑,因为它认为这是commplexAttribute

It's quite logic jaxb creates an inner class as it thinks that is a commplexAttribute

除了将其更改为布尔值之外,您还可以检查null == valueB如果您放入

Instead of changing it to a boolean you could also check null == valueB if you put

@XmlElement(name = "ValueB", nillable='true') protected Summary.ValueB valueB; 按照您的逻辑.

@XmlElement(name = "ValueB", nillable='true') protected Summary.ValueB valueB; in your logic.

或添加一个没有@XMl ..的额外的getter并返回valueB的计算状态 也许您想要的东西可以用JAXB来实现,而我以前并不需要它.

or add an extra getter that does not have @XMl.... and returns computed state of valueB perhaps what you want is possible with JAXB I have not needed it before.

这篇关于用JAXB将元素表示为布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆