首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JAXBElement<Boolean>与布尔

JAXBElement<Boolean>与布尔
EN

Stack Overflow用户
提问于 2012-10-03 17:06:22
回答 2查看 5.5K关注 0票数 6

JAXBElement Boolean到底是什么?我如何将其设置为布尔值与"true“的等价值?

方法:

代码语言:javascript
复制
  public void setIncludeAllSubaccounts(JAXBElement<Boolean> paramJAXBElement)
  {
    this.includeAllSubaccounts = paramJAXBElement;
  }

这做了而不是编译:

代码语言:javascript
复制
returnMessageFilter.setIncludeAllSubaccounts(true); 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-03 19:13:28

当JAXB (JSR-222)实现不能单独根据值判断该做什么时,就会生成JAXBElement作为模型的一部分。在您的示例中,您可能有一个元素,如:

代码语言:javascript
复制
<xsd:element 
    name="includeAllSubaccounts" type="xsd:boolean" nillable="true" minOccurs="0"/>

生成的属性不能是boolean,因为boolean不表示null。您可以将属性设置为Boolean,但是如何区分缺少的元素和使用xsi:nil设置的元素。这就是JAXBElement进来的地方。完整的例子见下文:

Foo

代码语言:javascript
复制
package forum12713373;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

    @XmlElementRef(name="absent")
    JAXBElement<Boolean> absent;

    @XmlElementRef(name="setToNull")
    JAXBElement<Boolean> setToNull;

    @XmlElementRef(name="setToValue")
    JAXBElement<Boolean> setToValue;

}

ObjectFactory

代码语言:javascript
复制
package forum12713373;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;

@XmlRegistry
public class ObjectFactory {

    @XmlElementDecl(name="absent")
    public JAXBElement<Boolean> createAbsent(Boolean value) {
        return new JAXBElement(new QName("absent"), Boolean.class, value);
    }

    @XmlElementDecl(name="setToNull")
    public JAXBElement<Boolean> createSetToNull(Boolean value) {
        return new JAXBElement(new QName("setToNull"), Boolean.class, value);
    }

    @XmlElementDecl(name="setToValue")
    public JAXBElement<Boolean> createSetToValue(Boolean value) {
        return new JAXBElement(new QName("setToValue"), Boolean.class, value);
    }

}

Demo

代码语言:javascript
复制
package forum12713373;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        ObjectFactory objectFactory = new ObjectFactory();

        Foo foo = new Foo();
        foo.absent = null;
        foo.setToNull = objectFactory.createSetToNull(null);
        foo.setToValue = objectFactory.createSetToValue(false);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }

}

输出

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo>
    <setToNull xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <setToValue>false</setToValue>
</foo>
票数 9
EN

Stack Overflow用户

发布于 2012-10-04 17:40:03

感谢NullUserException的评论,我能够在一行中实现这一点。这有点不同,所以我想为了别人的利益,我应该把它发出去。

代码语言:javascript
复制
returnMessageFilter.setIncludeAllSubaccounts(new JAXBElement<Boolean>(new QName("IncludeAllSubaccounts"), 
Boolean.TYPE, Boolean.TRUE));

澄清一下,QName是XmlElement标记名。

此外,还需要进口:

代码语言:javascript
复制
import javax.xml.bind.JAXBElement;

编辑

最好在ObjectFactory类中使用方便的方法,如布莱斯所建议的那样返回JAXBElement

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12713373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档