java dom4j我想在处理指令后消除空行
在java应用程序开发中,使用dom4j库进行xml输出。 我想在xml声明之后添加一个处理指令元素,然后写出一个普通元素
问题
我想消除在处理指令的元素之后插入空行。
FileOutputStream fos = null;
OutputFormat format = new OutputFormat(" ", true, "UTF-8");
XMLWriter writer = null;
try {
Document newDoc = DocumentHelper.createDocument();
Map<String, String> map = new LinkedHashMap<>();
// Add attributes of processing instruction to map
map.put("type", "text/xsl");
map.put("href", "../../test/sample.xsl");
// Generate processing instruction and add to dom
ProcessingInstruction pi = DocumentHelper.createProcessingInstruction("xml-stylesheet", map);
List<Node> headerList = newDoc.content();
headerList.add(0, pi);
// Add regular element to dom
Element person= newDoc.addElement("person");
person.addElement("name");
// xml export
fos = new FileOutputStream("C:\\demo\\demo.xml");
writer = new XMLWriter(fos, format);
writer.write(newDoc);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
演示。xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../../test/sample.xsl"?>
//👈I want to delete this blank line
<person>
<name/>
</person>
试试
即使OutputFormat的SETWEN LealEngress声明设置为false,如果XML声明后的空白行被删除,则如下:
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="../../test/sample.xsl"?>
//👈I want to delete this blank line
<person>
<name/>
</person>
此外,如果OutputFormat的setNewLineAfterDeclaration为false且没有指令处理,则不存在空行
<?xml version="1.0" encoding="UTF-8"?>
<person>
<name/>
</person>
处理指令后不能删除空行吗? 谢谢你的考虑
补充信息
dom4j版本:2.1.1
共 (0) 个答案