JAXB Marshaller没有值为null的元素 [英] JAXB Marshaller does not have elements whos value is null
问题描述
当我使用JAXB Marshaller编组java对象时,编组器不会为java对象中的空文件创建空元素。例如,我有一个以下java对象:
When I marshall a java object using JAXB Marshaller, the marshaller does not create empty elements for null files in the java object. For example, I have a following java object:
public class PersonTraining {
@XmlElement(name = "Val1", required = true)
protected BigDecimal val1;
@XmlElement(name = "Val2", required = true, nillable = true)
protected BigDecimal val2;
@XmlElement(name = "Val3", required = true, nillable = true)
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar val3;
}
当我获取此对象的实例并编组成XML时,我得到以下(这是因为我没有设置Val2的值):
When I take an instance of this object, and marshall into an XML, I get the following (This is beacuse I did not set the value for Val2):
<PersonTraining>
<Val1>1</Val1>
<Val3>2010-01-01T00:00:00.0-05:00</Val3>
</PersonTraining>
然而,我原本预计会有来自编组操作的结果(事实上,我特别需要元素)以便可以针对XSD验证XML)
However, I had expected hte following result from the marshalling operation (Infact, I specifically need element as well so that the XML can be validated against the XSD)
<PersonTraining>
<Val1>1</Val1>
<Val2></Val2>
<Val3>2010-01-01T00:00:00.0-05:00</Val3>
</PersonTraining>
请让我知道我需要设置哪个选项,以便对象属性中的空值可以也被编组,并作为空/ null元素返回。
Please let me know what option I would need to set so that the null value in the object attributes can ALSO be marshalled, and returned as empty/null elements.
这是编组代码:
StringWriter sw = new StringWriter();
JAXBContext jc = JAXBContext.newInstance("person_training");
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(ptl, sw);
推荐答案
默认情况下 JAXB(JSR-222) 实现不会为空值封送属性/元素。这将适用于Java模型中的以下字段。
By default a JAXB (JSR-222) implementation will not marshal an attribute/element for null values. This will be true for the following field in your Java model.
@XmlElement(name = "Val1", required = true)
protected BigDecimal val1;
您可以通过指定 nillable = true $ c $来覆盖此行为c>在
@XmlElement
注释上,就像你在这里完成的那样:
You can override this behaviour by specifying nillable=true
on the @XmlElement
annotation like you have done here:
@XmlElement(name = "Val2", required = true, nillable = true)
protected BigDecimal val2;
这将导致 xsi:nil =true
要杠杆的属性:
This will cause the xsi:nil="true"
attribute to be leverage:
<Val2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
更多信息:
- http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html
PersonTraining
自你要注释字段
你应该确保在类或包级别指定 @XmlAccessorType(XmlAccessType.FIELD)
(参见: http://blog.bdoughan.com/2011/ 06 / using-jaxbs-xmlaccessortype-to.html 。
Since you are annotating the fields
you should make sure you specify @XmlAccessorType(XmlAccessType.FIELD)
at the class or package level (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html).
import java.math.BigDecimal;
import javax.xml.bind.annotation.*;
import javax.xml.datatype.XMLGregorianCalendar;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class PersonTraining {
@XmlElement(name = "Val1", required = true)
protected BigDecimal val1;
@XmlElement(name = "Val2", required = true, nillable = true)
protected BigDecimal val2;
@XmlElement(name = "Val3", required = true, nillable = true)
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar val3;
}
演示代码
演示
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(PersonTraining.class);
PersonTraining pt = new PersonTraining();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(pt, System.out);
}
}
输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<personTraining>
<Val2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<Val3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</personTraining>
这篇关于JAXB Marshaller没有值为null的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!