How to generate end tag for empty element in XML u

2019-02-21 22:01发布

I'm generating XML using JAXB. But JAXB is generating an empty Tag closing it self. But my client want separate empty tag. I know both are equals but he is not agree with me. please any one suggest the solution. Thanks.

Sample code:

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

    // Setters and Gettres
}

Actual Output:

<currencyCode>GBP</currencyCode>
<discountValue/>
<setPrice/>
<spendLowerThreshold/>
<spendUpperThreshold/>
<discountApportionmentPercent>0.0</discountApportionmentPercent>
<discountApportionmentValue/>

Expected Output:

<currencyCode>GBP</currencyCode>
<discountValue></discountValue>
<setPrice></setPrice>
<spendLowerThreshold></spendLowerThreshold>
<spendUpperThreshold></spendUpperThreshold>
<discountApportionmentPercent>0.0</discountApportionmentPercent>
<discountApportionmentValue></discountApportionmentValue>

Code for Marshalling:

try {
    Marshaller marshaller = JAXBContext.newInstance(CountryData.class).createMarshaller();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    marshaller.marshal(countryData , os);
    log.debug("The PPV request raw XML -> " + os.toString());
} catch (JAXBException e) {
    // nothing to do
}

I'm Using JDK 6.0

4条回答
SAY GOODBYE
2楼-- · 2019-02-21 22:43

Problem solved, To force a closing tag, just add an empty String as the value of this tag ! You can do it using @XmlValue :

public class closedTag {

    @XmlValue
    private String content;


    public closedTag() {
        this.content = "";
    }

}
查看更多
时光不老,我们不散
3楼-- · 2019-02-21 22:58

This behaviour is implementation dependant. I stumbled into opposite problem (I needed self-closing tag) and I resolved in this way.
You can try using this version of marshal() or modifying code from linked answer.

查看更多
戒情不戒烟
4楼-- · 2019-02-21 23:00

If you have generated Classes from XSD then you would also generated ObjectFactory class. If don't please refer here about how to generate ObjectFactory class.

After that, your code would be like--

JAXBContext context;
            context = JAXBContext.newInstance(*yourClass*.class);

final ObjectFactory objFactory = new ObjectFactory();

            final JAXBElement<YourClass> element = objFactory
                    .*autoGeneratedmethodfromObjectFactorytogetelement*;

Marshaller marshaller;
            marshaller = context.createMarshaller();

            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                    Boolean.TRUE);
            final StringWriter stringWriter = new StringWriter();


            marshaller.marshal(element, stringWriter);
          String message = stringWriter.toString();

This will give you desired output.

查看更多
别忘想泡老子
5楼-- · 2019-02-21 23:04
I know both are equals but he is not agree with me.

He's wrong. As a software professional, you have to learn how to tell your client when he is wrong. If you were an electrician and your client asked you to wire a switch in an incorrect way, you would tell him that as a professional you had to comply with industry standards.

查看更多
登录 后发表回答