1

A sample code with jaxb-api 2.3.1 and Java 8 using StringWriter for 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 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?

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

Actual output:

<currencyCode>GBP</currencyCode>
<discountValue></discountValue>
<setPrice></setPrice>
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
rongmei
  • 33
  • 1
  • 5

2 Answers2

1

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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "currencyCode", "discountValue", "setPrice" })
@XmlRootElement(name = "countryData")
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

<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"/>
Lakshan
  • 1,404
  • 3
  • 10
  • 23
  • Thanks Lakshan. However, I need to have the output: without other attributes. So how to remove "xmlns:xsi" and "xsi:nil"? – rongmei Sep 03 '20 at 21:39
  • @rongmei this is not a Industry standard solution. but it fulfill your requirement. https://stackoverflow.com/a/47283792/8700934 – Lakshan Sep 05 '20 at 06:08
  • yes. That given linked solution would fulfill my requirement as a workaround. I still wonder any Lib can provide choice of self-closing tag. Seems many are looking for this option as well. – rongmei Sep 05 '20 at 12:46
  • @rongmei please refer this answer. https://stackoverflow.com/a/17366633/8700934 – Lakshan Sep 05 '20 at 12:53
  • great, thanks, the referred link does help me. I got self-closing tag now. Similar solution with cleaner doc: http://www.nocker.com.au/index.php/2019/05/16/jaxb-marshal-to-xml-json-printing-all-null-fields/ – rongmei Sep 06 '20 at 13:58
0

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:

<discountValue/>
<setPrice/>
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
  • Thanks Nickolas. Remove default values + don't set value, "setPrice" tag is removed from generated XML. If I set value null (in an object instance), "setPrice" tag also disappears. – rongmei Sep 03 '20 at 21:36
  • Nikolas, sorry, my previous comment was not clearly read. The suggestion removed empty tag completely. I don't get the output of . Till now, I still don't have working solution. – rongmei Sep 04 '20 at 08:58