Ben*_*est 5 java xml jaxb moxy
我正在使用JAXB / Moxy将类编组为XML。当根元素仅包含属性时,输出如下:
<?xml version="1.0" encoding="UTF-8"?>
<procedure xmlns="http://xml.test.com/api/request" name="TestProcedure"/>
Run Code Online (Sandbox Code Playgroud)
所需的输出包含过程的结束标记:
<?xml version="1.0" encoding="UTF-8"?>
<procedure xmlns="http://xml.test.com/api/request" name="TestProcedure"></procedure>
Run Code Online (Sandbox Code Playgroud)
这将被发送到第三方系统。即使两者都是格式正确的XML,它仍然需要结束标记。
我看到了这篇文章:JAXB在XML上将XML与OutputStream和StringWriter不同, 但是在本地运行时,在输出流和字符串之间没有看到输出差异。
这似乎只处理元素和属性,而不处理根元素: 在xml jaxb中将空值表示为空元素 我仍然设置了DescriptorCustomizer并在调试器中浏览了ClassDescriptor,但是没有看到像XMLDirectMapping。
我的域对象看起来像
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
我的服务包括:
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);
Run Code Online (Sandbox Code Playgroud)
是否有任何类似于Marshaller.JAXB_FORMATTED_OUTPUT的属性或属性可以设置为在结尾处强制使用结束标记?或其他建议?
由于这两种形式在语义上是等价的 XML:
我相信您会陷入困境,除非您可以使用允许自定义空标签的不同 JAXB 或封送拆收器实现。我认为撒克逊人曾经有过这样的选择,但多年来一直没有考虑过。
| 归档时间: |
|
| 查看次数: |
3237 次 |
| 最近记录: |