0
我有下面的代码,即插入根元素之前的处理操作的指令:序列化XML处理指令
Document doc = builder.parse(file);
doc.insertBefore(
doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"annotation.xsl\""),
doc.getDocumentElement());
doc.insertBefore(doc.createProcessingInstruction("oxygen", "NVDLSchema=\"annotation.nvdl\""),
doc.getDocumentElement());
和我使用它来序列化:
FileOutputStream fos = new FileOutputStream(new File(file.getAbsolutePath() + ".out"));
DOMImplementationLS ls = (DOMImplementationLS) builder.getDOMImplementation();
LSOutput lso = ls.createLSOutput();
lso.setByteStream(fos);
ls.createLSSerializer().write(doc, lso);
fos.close();
作为输出I得到:
<?xml version="1.0" encoding="UTF-8"?>
<fulltext-document>...</fulltext-document><?xml-stylesheet type="text/xsl" href="annotation.xsl"?><?oxygen NVDLSchema="annotation.nvdl"?>
但是我打算有处理指令之前根元素。我检查了可能是DOM三不正确(见下文),但一切看起来都不错。有什么我错过了吗?欢迎任何解决方案。
P.S.我使用Java 1.6.0_27 DOM。如果上面看起来像一个错误,欢迎链接到错误报告。
谢谢你的优雅的解决方案。 –