【发布时间】:2011-11-02 12:09:17
【问题描述】:
我有以下代码,在根元素之前插入处理指令:
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();
作为输出我得到:
<?xml version="1.0" encoding="UTF-8"?>
<fulltext-document>...</fulltext-document><?xml-stylesheet type="text/xsl" href="annotation.xsl"?><?oxygen NVDLSchema="annotation.nvdl"?>
但是我打算在 根元素之前有处理指令。我检查了也许 DOM 三个是不正确的(见下文),但一切看起来都很好。有什么我错过的吗?欢迎任何解决方案。
附:我使用 Java 1.6.0_27 DOM。如果上面看起来像一个错误,欢迎链接到错误报告。
【问题讨论】:
标签: java xml xml-serialization