JAXB 从 Java 生成 nillable = "true"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20076018/
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 generate nillable = "true" from java
提问by user1414745
it this a bug?
这是一个错误吗?
I need nillable = "true"
in my xsd schema. The only way to generate such an element from my java code is to use @XmlElement(nillable = true)
, right? But in this case, I will not be able to take advantage of this definition, I will not be able to check if the element is set to nil
. The method isNil()
is in the JAXBElement
wrapper class.
我需要nillable = "true"
在我的 xsd 架构中。从我的java代码生成这样一个元素的唯一方法是使用@XmlElement(nillable = true)
,对吗?但在这种情况下,我将无法利用这个定义,我将无法检查元素是否设置为nil
. 该方法isNil()
位于JAXBElement
包装类中。
So, what are my options here - I want to generate nillable = "true"
in my xsd schema AND be able to check if it is set from my java code.
那么,我在这里有什么选择 - 我想nillable = "true"
在我的 xsd 模式中生成并能够检查它是否是从我的 java 代码中设置的。
采纳答案by bdoughan
I need nillable = "true" in my xsd schema. The only way to generate such an element from my java code is to use @XmlElement(nillable = true), right?
我的 xsd 架构中需要 nillable = "true"。从我的java代码生成这样一个元素的唯一方法是使用@XmlElement(nillable = true),对吗?
Yes.
是的。
But in this case, I will not be able to take advantage of this definition, I will not be able to check if the element is set to nil. The method isNil() is in the JAXBElement wrapper class.
但在这种情况下,我将无法利用这个定义,我将无法检查元素是否设置为 nil。方法 isNil() 位于 JAXBElement 包装器类中。
You can do getFoo() == null
to determine if it was null. If you are trying to determine if the null corresponds to an absent element or xsi:nil="true"
then you will have to do more. A set won't be done for absent nodes so you can put logic in your setter to track if a set to null was done because of an element with xsi:nil="true
.
您getFoo() == null
可以确定它是否为空。如果您试图确定 null 是否对应于不存在的元素,xsi:nil="true"
那么您将不得不做更多的事情。不会为不存在的节点设置集合,因此您可以在设置器中放置逻辑以跟踪是否由于带有xsi:nil="true
.
@XmlElement(nillable=true)
public String getFooString() {
return fooString;
}
public void setFooString(String foo) {
this.fooString = foo;
this.setFoo = true;
}
If you don't want to have this extra logic (which doesn't help for marshalling anyways you need to leverage JAXBElement
.
如果您不想拥有这种额外的逻辑(这对编组无论如何都无济于事,您需要利用JAXBElement
.
@XmlElementRef(name="fooJAXBElement")
public JAXBElement<String> getFooJAXBElement() {
return fooJAXBElement;
}
public void setFooJAXBElement(JAXBElement<String> fooJAXBElement) {
this.fooJAXBElement = fooJAXBElement;
}
Java Model
Java模型
Root
根
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlType(propOrder={"fooString", "barString", "fooJAXBElement", "barJAXBElement"})
public class Root {
private String fooString;
private String barString;
private JAXBElement<String> fooJAXBElement;
private JAXBElement<String> barJAXBElement;
private boolean setFoo;
private boolean setBar;
@XmlElement(nillable=true)
public String getFooString() {
return fooString;
}
public void setFooString(String foo) {
this.fooString = foo;
this.setFoo = true;
}
public boolean isSetFooString() {
return setFoo;
}
@XmlElement(nillable=true)
public String getBarString() {
return barString;
}
public void setBarString(String bar) {
this.barString = bar;
this.setBar = true;
}
public boolean isSetBarString() {
return setBar;
}
@XmlElementRef(name="fooJAXBElement")
public JAXBElement<String> getFooJAXBElement() {
return fooJAXBElement;
}
public void setFooJAXBElement(JAXBElement<String> fooJAXBElement) {
this.fooJAXBElement = fooJAXBElement;
}
@XmlElementRef(name="barJAXBElement")
public JAXBElement<String> getBarJAXBElement() {
return barJAXBElement;
}
public void setBarJAXBElement(JAXBElement<String> barJAXBElement) {
this.barJAXBElement = barJAXBElement;
}
}
ObjectFactory
对象工厂
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
@XmlElementDecl(name="fooJAXBElement")
public JAXBElement<String> createFooJAXBElement(String string) {
return new JAXBElement<String>(new QName("fooJAXBElement"), String.class, string);
}
@XmlElementDecl(name="barJAXBElement")
public JAXBElement<String> createBarJAXBElement(String string) {
return new JAXBElement<String>(new QName("barJAXBElement"), String.class, string);
}
}
Demo
演示
Below is a full example to demonstrate the concepts discussed above.
下面是一个完整的例子来演示上面讨论的概念。
input.xml
输入文件
This document contains 2 elements explicitly marked with xsi:nil="true"
and 2 other mapped elements that are absent.
本文档包含 2 个显式标记的元素xsi:nil="true"
和 2 个不存在的其他映射元素。
<?xml version="1.0" encoding="UTF-8"?>
<root>
<barString xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<barJAXBElement xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</root>
Demo
演示
This demo code will read in the above XML and check whether the properties have been set as a result of the unmarshal.
此演示代码将读取上述 XML 并检查是否已设置作为解组结果的属性。
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class, ObjectFactory.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum20076018/input.xml");
Root root = (Root) unmarshaller.unmarshal(xml);
System.out.println("Was fooString set? " + root.isSetFooString());
System.out.println("Was barString set? " + root.isSetBarString());
System.out.println("Was fooJAXBElement set? " + (root.getFooJAXBElement() != null));
System.out.println("Was barJAXBElement set? " + (root.getBarJAXBElement() != null));
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
Output
输出
Below is the output from running the demo code. We see that all the is set checks are correct, but that the output only fully matches the input for the JAXBElement
properties.
下面是运行演示代码的输出。我们看到所有设置检查都是正确的,但输出仅与JAXBElement
属性的输入完全匹配。
Was fooString set? false
Was barString set? true
Was fooJAXBElement set? false
Was barJAXBElement set? true
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<fooString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<barString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<barJAXBElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</root>