用JAXB将元素表示为布尔值?

编程入门 行业动态 更新时间:2024-10-19 22:34:52
本文介绍了用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将元素表示为布尔值?

本文发布于:2023-11-17 07:09:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609255.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   布尔值   JAXB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!