Java 如果根元素只有一个属性,JAXB将不使用close标记封送它
我正在使用JAXB/Moxy将类封送到XML中。当根元素仅包含属性时,输出如下所示:Java 如果根元素只有一个属性,JAXB将不使用close标记封送它,java,xml,jaxb,moxy,Java,Xml,Jaxb,Moxy,我正在使用JAXB/Moxy将类封送到XML中。当根元素仅包含属性时,输出如下所示: <?xml version="1.0" encoding="UTF-8"?> <procedure xmlns="http://xml.test.com/api/request" name="TestProcedure"/> 是否有任何属性或属性可以设置为强制结束标记,类似于Marshaller.JAXB_格式的_输出? 还是其他建议?因为这两种形式在语义上是等价的XM
<?xml version="1.0" encoding="UTF-8"?>
<procedure xmlns="http://xml.test.com/api/request" name="TestProcedure"/>
是否有任何属性或属性可以设置为强制结束标记,类似于Marshaller.JAXB_格式的_输出?
还是其他建议?因为这两种形式在语义上是等价的XML:
我相信,除非您可以使用允许定制空标记的不同JAXB或封送拆收器实现,否则您将陷入困境。我想撒克逊人曾经有过这样的选择,但已经好几年没有考虑过了。吉姆用第二点引导我走向了最终的方向。我最终在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();
XMLOutputFactory outputFactory=XMLOutputFactory.newInstance();
Set tags=newhashset();
//这指定应允许过程字段使用自动关闭语法,而不是使用单独的结束标记
//标签。添加(“程序”);
setProperty(WstxOutputProperties.P_OUTPUT_EMPTY_ELEMENT_处理程序,新的EmptyElementHandler.SetEmptyElementHandler(标记));
StringWriter sw=新的StringWriter();
XMLStreamWriter writer=outputFactory.createXMLStreamWriter(sw);
writer.writeStartDocument(“1.0”);
书面启动条款(“程序”);
writer.writeAttribute(“name”,projectRequest.getName());
writeAttribute(“requestId”,projectRequest.getRequestId());
writer.writeedelement();
writer.writeEndDocument();
writer.flush();
sw.flush();
@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;
}
}
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);
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();