且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在使用JAXB进行编组时添加DOCTYPE和xml处理指令?

更新时间:2023-09-29 12:20:52

JAXB RI拥有专有的 Marshaller property com.sun.xml.bind.xmlHeaders (参见 XML Preamble Control

The JAXB RI has a proprietary Marshaller property com.sun.xml.bind.xmlHeaders (see XML Preamble Control:


此属性允许您要指定
XML前导码(<?xml ...>
声明)和任何其他PI,
条评论, DOCTYPE 声明
跟随它。只有当您编组为
时,此属性才会生效
OutputStream Writer ,或
StreamResult 。请注意,此
属性会互动使用
Marshaller.JAXB_FRAGMENT 属性。
如果该属性未被触及或将
设置为false,则JAXB将始终写入
它的XML前导码,所以这个属性可以只用
o写PI,评论,
DOCTYPE 等。另一方面,如果
设置为true,那么JAXB将不会是
编写自己的XML前导码,因此这个
属性可能包含自定义XML
前导码。

This property allows you to specify an XML preamble (<?xml ...> 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.

这应该做你需要什么。如果您正在使用Java5和JAXB RI,那么这应该可行。如果您使用Java6及其包含的JAXB实现,那么 com.sun.xml.bind.xmlHeaders 名称可能会有所不同,因此请尝试 com。而是sun.xml.internal.bind.xmlHeaders

This should do what you need. If you're using Java5 and the JAXB RI, then this should just work. If you're using Java6 with its included JAXB implementation, the com.sun.xml.bind.xmlHeaders name might be different, so try com.sun.xml.internal.bind.xmlHeaders instead.