如何使用JAXB在XML中为空元素生成自闭合标签<tag/>。

huangapple go评论87阅读模式
英文:

How to generate self-closing tag <tag/> for empty element in XML using JAXB

问题

使用jaxb-api 2.3.1和Java 8的示例代码,使用StringWriter进行jaxbMarshaller

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "currencyCode",
    "discountValue",
    "setPrice"
})
@XmlRootElement(name = "countryData")
public class CountryData {
    protected String currencyCode;
    protected String discountValue = "";
    protected String setPrice = "";

    // setters and getters
}

当我使用以下代码将实体编组为XML字符串时:

StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(countryDataObject, sw);
sw.toString();

如何获得空值的预期结果?

<currencyCode>GBP</currencyCode>
<discountValue/>
<setPrice/>

实际输出:

<currencyCode>GBP</currencyCode>
<discountValue></discountValue>
<setPrice></setPrice>
英文:

A sample code with jaxb-api 2.3.1 and Java 8 using StringWriter for jaxbMarshaller:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = &quot;&quot;, propOrder = {
    &quot;currencyCode&quot;,
    &quot;discountValue&quot;,
    &quot;setPrice&quot;
})
@XmlRootElement(name = &quot;countryData&quot;)
public class CountryData {
    protected String currencyCode;
    protected String discountValue = &quot;&quot;;
    protected String setPrice = &quot;&quot;;

    // setters and setters
}

When I marshal the entity to a XML string with:

StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(countryDataObject, sw);
sw.toString();

How to get expected result for empty values?

&lt;currencyCode&gt;GBP&lt;/currencyCode&gt;
&lt;discountValue/&gt;
&lt;setPrice/&gt;

Actual output:

&lt;currencyCode&gt;GBP&lt;/currencyCode&gt;
&lt;discountValue&gt;&lt;/discountValue&gt;
&lt;setPrice&gt;&lt;/setPrice&gt;

答案1

得分: 1

<currencyCode>GBP</currencyCode>
<discountValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<setPrice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
英文:

don't initialized the variables. use the nillable attribute and set it value to true

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = &quot;&quot;, propOrder = { &quot;currencyCode&quot;, &quot;discountValue&quot;, &quot;setPrice&quot; })
@XmlRootElement(name = &quot;countryData&quot;)
public class CountryData {
    @XmlElement(nillable=true)
    protected String currencyCode;
    @XmlElement(nillable=true)
    protected String discountValue;
    @XmlElement(nillable=true)
    protected String setPrice;
    // getters and setters
}

output

&lt;currencyCode&gt;GBP&lt;/currencyCode&gt;
&lt;discountValue xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:nil=&quot;true&quot;/&gt;
&lt;setPrice xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:nil=&quot;true&quot;/&gt;

答案2

得分: 0

虽然这些字符串是空的,但它们仍然包含非空数据,并且会生成结束标签。移除字符串的默认值,或将它们设置为 null(默认实例字段值):

protected String discountValue;
protected String setPrice;

标签会变成闭合形式:

<discountValue/>
<setPrice/>
英文:

Although the strings are empty they still contain non-null data and the end tag is generated. Remove the default values of the strings or set them as null (a default instance field value):

protected String discountValue;
protected String setPrice;

The tags become closed:

&lt;discountValue/&gt;
&lt;setPrice/&gt;

huangapple
  • 本文由 发表于 2020年9月2日 03:38:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63694347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定