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

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

How to add DOCTYPE and xml processing instructions when marshalling with JAXB?

javaxmljaxbmarshalling

提问by Juha Syrj?l?

I am marshalling (serializing) JAXB beans to output stream. How can I add DOCTYPE declaration and xml processing instructions to ouput?

我正在将 JAXB bean 编组(序列化)到输出流。如何在输出中添加 DOCTYPE 声明和 xml 处理指令?

I am doing currently marshalling like this:

我目前正在做这样的编组:

JAXBContext jaxbContext = JAXBContext.newInstance("com.example.package");
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = schemaFactory.newSchema(schemaSource);
marshaller.setSchema(schema);

marshaller.marshal(object, output);

I'd like have output that looks something like this:

我想要输出看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Something SYSTEM "some.dtd">
<?xml-stylesheet type="text/xsl" href="some.xsl"?>

JAXB bean are generated code so I don't want to change them.

JAXB bean 是生成的代码,所以我不想更改它们。

There are some hacks and undocumented tricks (see Making JAXB generate an XML processing instruction) to add the xml processing instructionsand doctype. But what is the preferred or right way to do this?

有一些技巧和未公开的技巧(请参阅使 JAXB 生成 XML 处理指令)来添加xml 处理指令doctype。但是,这样做的首选或正确方法是什么?

回答by skaffman

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

JAXB RI 具有专有Marshaller属性com.sun.xml.bind.xmlHeaders(请参阅XML Preamble Control

This property allows you to specify an XML preamble (<?xml ...>declaration) and any additional PIs, comments, DOCTYPEdeclaration 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_FRAGMENTproperty. 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声明。此属性只有当你编组到效果 OutputStreamWriterStreamResult。请注意,此属性与属性交互 Marshaller.JAXB_FRAGMENT。如果该属性未修改或设置为 false,则 JAXB 将始终编写其 XML 前导码,因此该属性只能用于编写 PI、注释 DOCTYPE等。另一方面,如果设置为 true,则 JAXB 将不编写自己的 XML 前导,因此此属性可能包含自定义 XML 前导。

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.xmlHeadersname might be different, so try com.sun.xml.internal.bind.xmlHeadersinstead.

这应该做你需要的。如果您使用的是 Java5 和 JAXB RI,那么这应该可以正常工作。如果您使用 Java6 及其包含的 JAXB 实现,则com.sun.xml.bind.xmlHeaders名称可能不同,因此请com.sun.xml.internal.bind.xmlHeaders改用。