2012-10-03 51 views
6

究竟是JAXBElement Boolean,我怎麼可以設置爲的「true」布爾相同呢?的JAXBElement <Boolean> VS布爾

方法:

public void setIncludeAllSubaccounts(JAXBElement<Boolean> paramJAXBElement) 
    { 
    this.includeAllSubaccounts = paramJAXBElement; 
    } 

這並不編譯:

returnMessageFilter.setIncludeAllSubaccounts(true); 
+0

如果該方法需要一個['JAXBElement'(http://docs.oracle.com/javaee/5/api/javax/xml/bind/JAXBElement.html),爲什麼你想傳遞TRUE; ,布爾? – NullUserException

+1

你可以在你有問題和堆棧跟蹤的地方加入代碼嗎? –

+1

創建的JAXBElement,如:'JAXBElement的 jaxtrue =新的JAXBElement(QName的,Boolean.TYPE,Boolean.TRUE);'和傳遞 – NullUserException

回答

8

一個JAXBElement作爲模型的一部分,當一個JAXB(JSR-222)的實施將不能夠產生單獨根據價值告訴該做什麼。在您的例子,你可能不得不像一個元素:

<xsd:element 
    name="includeAllSubaccounts" type="xsd:boolean" nillable="true" minOccurs="0"/> 

生成的屬性不能boolean因爲boolean不代表null。你可以做財產Boolean但後來你怎麼區分是一個缺少的元素,並與xsi:nil設置的元素。這是JAXBElement的進來見下面一個完整的例子:

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

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); 
    } 

} 

演示

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); 
    } 

} 

輸出

<?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> 
1

由於NullUserException的評論,我能在一行實現這一點。它有些不同,所以我想我會把它發佈給其他人。

returnMessageFilter.setIncludeAllSubaccounts(new JAXBElement<Boolean>(new QName("IncludeAllSubaccounts"), 
Boolean.TYPE, Boolean.TRUE)); 

爲了澄清,QName是XmlElement標籤名稱。

此外,需要進口:

import javax.xml.bind.JAXBElement; 

編輯

最好使用便捷方法ObjectFactory類返回JAXBElement爲布萊斯建議。

+1

僅供參考 - 當你的生成的模型的一部分,應該有一個包含一個方便的方法與適當的'QName'創造必要的'JAXBElement'的'ObjectFactory'類。 –

+1

哦,是的,有。我認爲我不需要從藍色創建一個。謝謝! – Jade