JAXB Marshals root element without close tag if it has only attribute

I am using JAXB / Moxy to marshal a class in XML. When the root element contains only the attribute, the output is as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <procedure xmlns="http://xml.test.com/api/request" name="TestProcedure"/>

The required output contains a closing tag for the procedure:

   <?xml version="1.0" encoding="UTF-8"?>
   <procedure xmlns="http://xml.test.com/api/request" name="TestProcedure"></procedure>

This is sent to a third-party system. Although both are well-formed XML, it still needs a closing tag.

I saw this post: JAXB organizes XML differently with OutputStream and StringWriter but did not see the difference in output between the output stream and strings when running locally.

This, apparently, applies only to elements and attributes, but not to the root element: Represent a null value as an empty element in xml jaxb I still configured DescriptorCustomizer and looked at the ClassDescriptor in the debugger, but did not see any properties that can be set as XMLDirectMapping.

My domain object looks like

   @XmlRootElement(name = "procedure")
   public class ProcRequest {
    protected String procName;
    protected String requestId;
    protected List<Param> parameter;

    @XmlAttribute
    public String getProcName() {
        return procName;
    }
    public void setProcName(String procName) {
        this.procName = procName;
    }
    @XmlAttribute
    public String getRequestId() {
        return requestId;
    }
    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }
    @XmlElement
    public List<Param> getParam() {
        if (this.param == null) {
            this.param = new ArrayList<Param>();
        } 

        return param;
    }
    public void setParam(List<Param> param) {
        this.param = param;
    }

   }

And my service contains:

   ProcRequest procRequest = new ProcRequest();
   procRequest.setProcName("TestProcedure");

   JAXBContext jaxbContext = JAXBContext.newInstance(ProcRequest.class);

   Marshaller moxyMarshaller = jaxbContext.createMarshaller();
   moxyMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

   moxyMarshaller.marshal(procRequest, System.out);

Are there any properties or attributes that can be set to force the closing tag to close at the end, similar to Marshaller.JAXB_FORMATTED_OUTPUT? Or any other suggestions?

+4
source share
2 answers

Since the two forms are semantically equivalent to XML:

  • JAXB ;
  • XML.

, , JAXB , . , - , .

+3

2. Woodstox. , . , .

XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
Set<String> tags = new HashSet<String>();
// This specifies the procedure field should be allowed to use the self-closing syntax instead of having a separate end tag
//tags.add("procedure"); 
outputFactory.setProperty(WstxOutputProperties.P_OUTPUT_EMPTY_ELEMENT_HANDLER, new EmptyElementHandler.SetEmptyElementHandler(tags));  
StringWriter sw = new StringWriter();
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(sw);

writer.writeStartDocument("1.0");
writer.writeStartElement("procedure");
writer.writeAttribute("name", procRequest.getName());
writer.writeAttribute("requestId", procRequest.getRequestId());
writer.writeEndElement();
writer.writeEndDocument();
writer.flush();
sw.flush();
0

All Articles