使用JAXB编组时如何添加DOCTYPE和xml处理指令? [英] How to add DOCTYPE and xml processing instructions when marshalling with JAXB?

查看:43
本文介绍了使用JAXB编组时如何添加DOCTYPE和xml处理指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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 是生成的代码,所以我不想更改它们.

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

有一些黑客和未公开的技巧(参见 使 JAXB 生成 XML 处理指令)添加xml处理指令文档类型.但这样做的首选或正确方法是什么?

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

推荐答案

JAXB RI 具有专有的 Marshaller 属性 com.sun.xml.bind.xmlHeaders(请参阅XML 前导控制:

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

这个属性允许你指定一个XML 序言 (声明)和任何其他 PI,注释,DOCTYPE 声明跟随它.此属性生效只有当你编组到OutputStreamWriter流结果.请注意,这属性与Marshaller.JAXB_FRAGMENT 属性.如果该属性未更改或设置为 false,则 JAXB 将始终写入它的 XML 序言,所以这个属性可以仅用于写 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.

这篇关于使用JAXB编组时如何添加DOCTYPE和xml处理指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆