java - `@XmlRootElement` 和 `nillable`

标签 java xml jaxb jaxb2

有没有办法让 JAXB 正确打印 xmlns:xsixsi:nill在 nillable @XmlRootElement

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>

我只想知道有什么办法让第一个<value/>武装xmlns:xsixsi:nill .

最佳答案

注意:我是 EclipseLink JAXB (MOXy)领导,并且是 JAXB (JSR-222) 的成员专家组。

我不相信有一种方法可以使用标准的 JAXB API 来做到这一点。下面是一个示例,其中可以通过利用 @XmlElement(nillable=true)@XmlPath("text()") 来获得所需的行为。

值(value)

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

要将 MOXy 指定为您的 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"/>

关于java - `@XmlRootElement` 和 `nillable`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11796699/

相关文章:

java - Android Ripple 效果在 fragment 内滞后

c++ - 使用快速 xml 查找子元素

java - 使用 JAXB 防止 XXE 攻击

java - 使用 @XmlAnyElement 的 HashMap JAXB 映射

尝试从可运行的 jar 文件加载图像文件时出现 javax.imageio.IIOException

java - maven - spring 项目构建配置文件

java - 通过自定义 Tomcat Webapp Loader 访问嵌入式资源

xml - 省略额外的 xlmns 和 xmlns :tei that appear when I apply my transformation

java - 使用 Java 和 RichFaces 构建 XML 编辑器

java - 如何使用 JAXB 将属性添加到 HashMap?