java 将 XML 注释添加到封送文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1347534/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:12:40  来源:igfitidea点击:

Add XML comments into marshaled file

javaxmljaxb

提问by Oleksandr

I'm marshaling objects into an XML file. How can I add comments into that XML file?

我正在将对象编组到 XML 文件中。如何在该 XML 文件中添加注释?

采纳答案by laz

I do not see a way to do it with JAXB alone. However, I think you can leverage DOM to get the desired effect:

我看不到单独使用 JAXB 的方法。但是,我认为您可以利用 DOM 来获得所需的效果:

final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document doc = builder.getDOMImplementation().createDocument(null, null, null);

final Binder<Node> binder = jaxbContext.createBinder();
binder.marshal(jaxbObject, doc);
final Comment comment = doc.createComment("This is a comment");
doc.appendChild(comment);

final DOMSource domSource = new DOMSource(doc);
// use System.out for testing
final StreamResult streamResult = new StreamResult(System.out);
final TransformerFactory tf = TransformerFactory.newInstance();
final Transformer serializer = tf.newTransformer();
serializer.transform(domSource, streamResult);

Where jaxbContext is the JAXBContext object you are working with and jaxbObject is the object to be marshalled. This sample just appends the comment to the end of the document. For a different location, you would have to traverse the DOM through the doc object or use XPath to find the exact element you want the comment added to and use appendChild on it.

其中 jaxbContext 是您正在使用的 JAXBContext 对象,而 jaxbObject 是要编组的对象。此示例只是将注释附加到文档的末尾。对于不同的位置,您必须通过 doc 对象遍历 DOM 或使用 XPath 找到您想要添加注释的确切元素并在其上使用 appendChild。

回答by DanEEStar

You can add comments right after the preamble with the proprietary Marshaller property com.sun.xml.bind.xmlHeaders (see XML Preamble Control)

您可以使用专有的 Marshaller 属性 com.sun.xml.bind.xmlHeaders 在序言之后添加注释(请参阅 XML 序言控制

In the included JAXB-implementation jdk1.6.0_29 the property is called "com.sun.xml.internal.bind.xmlHeaders"

在包含的 JAXB 实现 jdk1.6.0_29 中,该属性称为“com.sun.xml.internal.bind.xmlHeaders”

See also question: How to add DOCTYPE and xml processing instructions when marshalling with JAXB?

另请参见问题:使用 JAXB 编组时如何添加 DOCTYPE 和 xml 处理指令?

So to get this XML with the test-comment after the preamble:

因此,要在序言之后使用测试注释获取此 XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Test Comment -->
<player>
    <name>Daniel</name>
    <birthday>1982-06-09T00:00:00+02:00</birthday>
</player>

You can use this Java-Code:

您可以使用此 Java 代码:

JAXBContext context = JAXBContext.newInstance(Player.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty("com.sun.xml.internal.bind.xmlHeaders", "\n<!-- Test Comment -->");
m.marshal(player, System.out);

回答by kmp

If anyone comes to this now, like I just did, it is worth pointing out that the property to do this is now com.sun.xml.bind.xmlHeaders(no longer internal it seems), so you can solve the problem like this (I have only tried it with EclipseLink MOXY):

如果现在有人来解决这个问题,就像我刚刚做的那样,值得指出的是现在执行此操作的属性com.sun.xml.bind.xmlHeaders(似乎不再是内部的),因此您可以像这样解决问题(我只用 EclipseLink MOXY 尝试过) ):

JAXBContext context = JAXBContext.newInstance(Player.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty("com.sun.xml.bind.xmlHeaders", "\n<!-- Test Comment -->");
m.marshal(player, System.out);

The following information originally comes from Marshaller Propertiesin the JAXB RI Extensionsdocumentation on jaxb.java.net:

以下信息最初来自jaxb.java.netJAXB RI 扩展文档中的Marshaller 属性

XML Preamble Control

This property allows you to specify an XML preamble ( declaration) and any additional PIs, comments, DOCTYPE declaration that follows it. This property takes effect only when you are marshalling to OutputStream, Writer, or StreamResult. Note that this property interacts with the Marshaller.JAXB_FRAGMENT property. If that property is untouched or set to false, then JAXB would always write its XML preamble, so this property can be only used to write PIs, comments, DOCTYPE, etc. On the other hand, if it is set to true, then JAXB will not write its own XML preamble, so this property may contain custom XML preamble.

XML 前导控制

此属性允许您指定 XML 前导(声明)和任何其他 PI、注释、跟在它后面的 DOCTYPE 声明。此属性仅在您编组到 OutputStream、Writer 或 StreamResult 时生效。请注意,此属性与 Marshaller.JAXB_FRAGMENT 属性交互。如果该属性未修改或设置为 false,则 JAXB 将始终编写其 XML 前导码,因此该属性只能用于编写 PI、注释、DOCTYPE 等。另一方面,如果设置为 true,则 JAXB不会编写自己的 XML 前导,因此此属性可能包含自定义 XML 前导。