2012-08-03 53 views
2

有沒有什麼辦法讓JAXB正確打印xmlns:xsixsi:nill在nillable上@XmlRootElement`@ XmlRootElement`和`nillable`

public class XmlValueTest { 

    public static void main(final String[] args) throws JAXBException { 

     final JAXBContext context = 
      JAXBContext.newInstance(Wrapper.class, Value.class); 

     final Marshaller marshaller = context.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 

     marshaller.marshal(Value.newInstance(null), System.out); 
     marshaller.marshal(Value.newInstance("null"), System.out); 
     marshaller.marshal(Wrapper.newInstance(null), System.out); 
     marshaller.marshal(Wrapper.newInstance("null"), System.out); 
    } 
} 

@XmlAccessorType(XmlAccessType.NONE) 
@XmlRootElement 
class Value { 

    public static Value newInstance(final String raw) { 
     final Value instance = new Value(); 
     instance.raw = raw; 
     return instance; 
    } 

    @XmlValue 
    private String raw; 
} 

@XmlAccessorType(XmlAccessType.NONE) 
@XmlRootElement 
class Wrapper { 

    public static Wrapper newInstance(final String raw) { 
     final Wrapper wrapper = new Wrapper(); 
     wrapper.raw = raw; 
     return wrapper; 
    } 

    @XmlElement(nillable = true, required = true) 
    private String raw; 
} 

打印

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<value/> <!-- is this normal? --> 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<value>null</value> 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<wrapper> 
    <raw xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
</wrapper> 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<wrapper> 
    <raw>null</raw> 
</wrapper> 

我只是想知道有沒有什麼辦法讓與xmlns:xsixsi:nill武裝第一<value/>

+1

親愛的布萊斯Doughan先生。你可以在'@ XmlRootElement'上添加'nillable'屬性嗎? – 2012-08-03 13:34:28

+0

這是一個有趣的請求。可以將此請求通過電子郵件發送給users @ jaxb.java.net'? – 2012-08-03 14:26:04

回答

1

備註:我是EclipseLink JAXB (MOXy)的領導者,也是JAXB (JSR-222)專家組的成員。

我不認爲有一種方法可以使用標準的JAXB API來完成此操作。下面是一個例子,它可以通過利用@XmlElement(nillable=true)@XmlPath("text()")來獲得所需的行爲。

價值

package forum11796699; 

import javax.xml.bind.annotation.*; 
import org.eclipse.persistence.oxm.annotations.XmlPath; 

@XmlRootElement 
public class Value { 

    private String value; 

    @XmlElement(nillable=true) 
    @XmlPath("text()") 
    public String getValue() { 
     return value; 
    } 

    public void setValue(String value) { 
     this.value = value; 
    } 

} 

jaxb.properties

要指定莫西爲您的JAXB提供者,你需要有一個在同一個包稱爲jaxb.properties與以下條目域模型文件(見:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 

演示

package forum11796699; 

import javax.xml.bind.*; 

public class Demo { 

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

     Value value = new Value(); 
     value.setValue(null); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.marshal(value, System.out); 
    } 

} 

輸出

<?xml version="1.0" encoding="UTF-8"?> 
<value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>