相关疑难解决方法(0)

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

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

我正在做这样的编组:

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);
Run Code Online (Sandbox Code Playgroud)

我希望输出看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Something SYSTEM "some.dtd">
<?xml-stylesheet type="text/xsl" href="some.xsl"?>
Run Code Online (Sandbox Code Playgroud)

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

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

java xml jaxb marshalling

14
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×1

jaxb ×1

marshalling ×1

xml ×1