Java 更改 JAXB 编组器生成的 XML 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18451870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Altering the XML header produced by the JAXB marshaller
提问by JME
I am currently using the following code to marshal an object into an xml string
我目前正在使用以下代码将对象编组为 xml 字符串
JAXBContext context;
try {
context = JAXBContext.newInstance(heartbeat.getClass());
StringWriter writer = new StringWriter();
Marshaller marshaller = context.createMarshaller();
heartbeat.setHeader(header);
heartbeat.setHeartbeatEvent(event);
marshaller.marshal(heartbeat, writer);
String stringXML = writer.toString();
return stringXML;
} catch (JAXBException e) {
throw new RuntimeException("Problems generating XML in specified "
+ "encoding, underlying problem is " + e.getMessage(),
e);
}
Which produces the following header
产生以下标题
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
My desired output is the following
我想要的输出如下
<?xml version=\"1.0\"?>
By adding this to the marshaller
通过将其添加到编组器
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\"?>");
I receive
我收到
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><?xml version="1.0"?>
and changing the JAXB_FRAGMENT property to TRUE removes the header entirely. I have been following the JAXB - Remove 'standalone="yes"' from generated XMLthread attempting to solve the problem but I have had no luck so far. Can someone please give me some insight on how to get my desired header from the JAXB marshaller?
并将 JAXB_FRAGMENT 属性更改为 TRUE 会完全删除标头。我一直在关注JAXB - 从生成的 XML线程中删除 'standalone="yes"'试图解决这个问题,但到目前为止我没有运气。有人能给我一些关于如何从 JAXB 编组器获取我想要的标头的见解吗?
采纳答案by bdoughan
When marshalling to an OutputStream
using a combination of the following produces the expected output.
当编组到OutputStream
使用以下组合时会产生预期的输出。
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\"?>");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
The problem you are seeing occurs when you marshal to a Writer
, which appears to be a bug in the JAXB reference implementation. You can raise an issue at the link below:
当您编组到 时会出现您所看到的问题Writer
,这似乎是 JAXB 参考实现中的一个错误。您可以通过以下链接提出问题:
You could always do:
你总是可以这样做:
JAXBContext context;
try {
context = JAXBContext.newInstance(heartbeat.getClass());
StringWriter writer = new StringWriter();
writer.append("<?xml version=\"1.0\"?>");
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
heartbeat.setHeader(header);
heartbeat.setHeartbeatEvent(event);
marshaller.marshal(heartbeat, writer);
String stringXML = writer.toString();
return stringXML;
} catch (JAXBException e) {
throw new RuntimeException("Problems generating XML in specified "
+ "encoding, underlying problem is " + e.getMessage(),
e);
}
EclipseLink JAXB (MOXy)also supports the com.sun.xml.bind.xmlHeaders
and it works correctly when marshalling to a Writer
(I'm the MOXy lead)
EclipseLink JAXB (MOXy)也支持com.sun.xml.bind.xmlHeaders
并且在编组到 a 时它可以正常工作Writer
(我是 MOXy 的负责人)
回答by Gopi Krishna
This worked for me
这对我有用
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");