Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 强制JAXB将空元素解释为Null而不是空字符串_Java_Xml_Jaxb - Fatal编程技术网

Java 强制JAXB将空元素解释为Null而不是空字符串

Java 强制JAXB将空元素解释为Null而不是空字符串,java,xml,jaxb,Java,Xml,Jaxb,在XSD模式中有一个特定元素,我希望JAXB将空元素内容视为null,而不是空字符串。模型类由XJC生成 我已经看到了,我被JAXB的RI困住了,所以我猜这对我不起作用 考虑到我只需要一个特定的元素,还有其他方法可以使用吗?因为这只是一个元素,下面是一种方法,可以让它与任何JAXB实现一起工作 Java模型 Foo import java.io.File; import javax.xml.bind.*; public class Demo { public static void

在XSD模式中有一个特定元素,我希望JAXB将空元素内容视为null,而不是空字符串。模型类由XJC生成

我已经看到了,我被JAXB的RI困住了,所以我猜这对我不起作用


考虑到我只需要一个特定的元素,还有其他方法可以使用吗?

因为这只是一个元素,下面是一种方法,可以让它与任何JAXB实现一起工作

Java模型 Foo

import java.io.File;
import javax.xml.bind.*;

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum18611294/input.xml");
        Foo foo = (Foo) unmarshaller.unmarshal(xml);

        System.out.println(foo.getBar());

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

}
通过利用
@xmlacessortype
注释,将类设置为使用字段访问。然后将与元素对应的字段初始化为
。实现
get
/
set
方法,将
视为
null

import javax.xml.bind.annotation.*;

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

    private String bar = "";

    public String getBar() {
        if(bar.length() == 0) {
            return null;
        } else {
            return bar;
        }
    }

    public void setBar(String bar) {
        if(null == bar) {
            this.bar = "";
        } else {
            this.bar = bar;
        }
    }

}
演示代码 下面是一些演示代码,您可以运行这些代码来查看一切是否正常

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<foo>
    <bar/>
</foo>
输出

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

因为这只针对一个元素,下面是一种方法,您可以将其用于任何JAXB实现

Java模型 Foo

import java.io.File;
import javax.xml.bind.*;

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum18611294/input.xml");
        Foo foo = (Foo) unmarshaller.unmarshal(xml);

        System.out.println(foo.getBar());

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

}
通过利用
@xmlacessortype
注释,将类设置为使用字段访问。然后将与元素对应的字段初始化为
。实现
get
/
set
方法,将
视为
null

import javax.xml.bind.annotation.*;

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

    private String bar = "";

    public String getBar() {
        if(bar.length() == 0) {
            return null;
        } else {
            return bar;
        }
    }

    public void setBar(String bar) {
        if(null == bar) {
            this.bar = "";
        } else {
            this.bar = bar;
        }
    }

}
演示代码 下面是一些演示代码,您可以运行这些代码来查看一切是否正常

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<foo>
    <bar/>
</foo>
输出

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

感谢您花时间回复Blaise-我应该提到模型类是由XJC生成的。是否有一些秘密绑定酱可以用来达到同样的效果?感谢您花时间回复Blaise-我应该提到模型类是由XJC生成的。有没有什么秘方可以达到同样的效果?