java JAXB 返回 null 而不是空字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10869748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAXB return null instead empty string
提问by MyTitle
How I can retrieve null
value, when unmarshalling, if inside XML attribute value is empty ? Now I make inside my getters checking for null
:
null
如果内部 XML 属性值为空,如何在解组时检索值?现在我在我的吸气剂中检查null
:
public String getLabel() {
if (label.isEmpty()) {
return null;
}
else {
return label;
}
}
But may be exist some other, more elegant way?
但可能存在其他一些更优雅的方式?
Thanks.
谢谢。
回答by npe
I think your XML looks more or less like this:
我认为您的 XML 或多或少是这样的:
<myElement></myElement>
This, unfortunately, means, that you are passing an empty string.
不幸的是,这意味着您正在传递一个空字符串。
If you want to pass null
you have two options:
如果你想通过,null
你有两个选择:
- Do not pass this tag at all (your XML should not contain
<myElement/>
tag at all). - Use
xsi:nil
.
- 根本不要传递这个标签(你的 XML 根本不应该包含
<myElement/>
标签)。 - 使用
xsi:nil
.
If using xsi:nil
, first you have to declare your xml element (in XSD file) as nilable
, like this:
如果使用xsi:nil
,首先必须将 xml 元素(在 XSD 文件中)声明为nilable
,如下所示:
<xsd:element name="myElement" nillable="true"/>
Then, to pass the null
value inside XML do this:
然后,要null
在 XML 中传递值,请执行以下操作:
<myElement xsi:nil="true"/>
or this:
或这个:
<myElement xsi:nil="true"></myElement>
This way, JAXB knows, that you are passing null
instead of an empty String.
这样,JAXB 就知道您正在传递null
而不是空字符串。
回答by bdoughan
The answer given by npeis a good one, and specifying how you want null
represented would be my recommendation as well. To have xsi:nil
marshalled you will want to annotate your property as (see Binding to JSON & XML - Handling Null):
npe 给出的答案很好,并且指定您想要的null
表示方式也是我的建议。要进行xsi:nil
编组,您需要将您的属性注释为(请参阅绑定到 JSON 和 XML - 处理空值):
@XmlElement(nillable=true)
public String getLabel() {
return label;
}
If you don't want to change your XML representation then you could use an XmlAdapter
:
如果您不想更改 XML 表示,则可以使用XmlAdapter
:
EmptyStringAdapter
空字符串适配器
package forum10869748;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class EmptyStringAdapter extends XmlAdapter<String, String> {
@Override
public String unmarshal(String v) throws Exception {
if("".equals(v)) {
return null;
}
return v;
}
@Override
public String marshal(String v) throws Exception {
return v;
}
}
Foo
富
You reference an XmlAdapter
through the use of the @XmlJavaTypeAdapter
annotation. If you would like this XmlAdapter
applied to all Strings then you could register it at the package level (see JAXB and Package Level XmlAdapters).
您可以XmlAdapter
通过使用@XmlJavaTypeAdapter
注释来引用 an 。如果您希望这XmlAdapter
适用于所有字符串,那么您可以在包级别注册它(请参阅JAXB 和包级别 XmlAdapters)。
package forum10869748;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement
public class Foo {
private String label;
@XmlJavaTypeAdapter(EmptyStringAdapter.class)
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
Demo
演示
package forum10869748;
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/forum10869748/input.xml");
Foo foo = (Foo) unmarshaller.unmarshal(xml);
System.out.println(foo.getLabel());
}
}
input.xml
输入文件
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<label></label>
</foo>
Output
输出
null