Java 在根节点之前的xml文件中添加换行符
我试图在XML文档中根节点上方的注释后添加换行符 我需要这样的东西:Java 在根节点之前的xml文件中添加换行符,java,xml,line-breaks,domparser,Java,Xml,Line Breaks,Domparser,我试图在XML文档中根节点上方的注释后添加换行符 我需要这样的东西: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!--DO NOT EDIT THIS FILE--> <projects> </projects> 基本上,您希望文本节点在注释节点后包含换行符 Element docElem = doc.getDocumentElement(); doc.insertBefor
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--DO NOT EDIT THIS FILE-->
<projects>
</projects>
基本上,您希望文本节点在注释节点后包含换行符
Element docElem = doc.getDocumentElement();
doc.insertBefore(doc.createComment("DO NOT EDIT THIS FILE"), docElem);
doc.insertBefore(doc.createTextNode("\\n"), docElem);
EDIT:似乎在
org.w3c.dom.Document
的根节点上不允许仅添加空格文本节点。这在形式上是100%正确的,但也无济于事
在转换器的输出中呈现注释的方式取决于它使用的序列化程序(HTML、XML和纯文本输出有不同的序列化程序)。在内置的XML序列化程序中,注释的结尾定义为-->
-不带换行符
由于javax.xml.transform.Transformer
的内部是硬连接的,因此序列化程序不是公共API,类被标记为final
,因此不可能重写该行为或设置自定义序列化程序
换言之,以干净的方式添加换行符是不走运的
但是,您可以安全地以稍微不干净的方式添加它:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
FileInputStream inputXml = new FileInputStream(new File("input.xml"));
Document doc = db.parse(inputXml);
// add the comment node
doc.insertBefore(doc.createComment("THIS IS A COMMENT"), doc.getDocumentElement());
StringWriter outputXmlStringWriter = new StringWriter();
Transformer transformer = transformerFactory.newTransformer();
// "xml" + "UTF-8" "include XML declaration" is the default anyway, but let's be explicit
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(doc), new StreamResult(outputXmlStringWriter));
// now insert our newline into the string & write an UTF-8 file
String outputXmlString = outputXmlStringWriter.toString()
.replaceFirst("<!--", "\n<!--").replaceFirst("-->", "-->\n");
FileOutputStream outputXml = new FileOutputStream(new File("output.xml"));
outputXml.write(outputXmlString.getBytes("UTF-8"));
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
FileInputStream inputXml=新的FileInputStream(新文件(“input.xml”);
Document doc=db.parse(inputXml);
//添加注释节点
doc.insertBefore(doc.createComment(“这是一条注释”)、doc.getDocumentElement();
StringWriter outputXmlStringWriter=新建StringWriter();
Transformer Transformer=transformerFactory.newTransformer();
//“xml”+“UTF-8”“include-xml-declaration”是默认值,但让我们明确一点
setOutputProperty(OutputKeys.METHOD,“xml”);
setOutputProperty(OutputKeys.OMIT_XML_声明,“no”);
transformer.setOutputProperty(OutputKeys.ENCODING,“UTF-8”);
transform(新的DOMSource(doc)、新的StreamResult(outputXmlStringWriter));
//现在将新行插入字符串并编写一个UTF-8文件
String outputXmlString=outputXmlStringWriter.toString()
.replaceFirst(“,”-->\n”);
FileOutputStream outputXml=新的FileOutputStream(新文件(“output.xml”);
write(outputXmlString.getBytes(“UTF-8”);
一般来说,不鼓励对XML字符串执行搜索和替换操作,但在这种情况下,几乎不会出错。过了一段时间后,因为我遇到了同样的问题,所以再次讨论这个问题。我找到了另一个不需要在字符串中缓冲输出的解决方案:
通过传递空文档仅写入XML声明。这还将附加一个换行符
编写没有XML声明的文档内容
代码:
您可以通过不向文档中添加注释节点,而是部分转换文档来实现这一点。首先分别转换您自己的XML处理指令和注释,然后转换文档的其余部分:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new FileInputStream(new File("abc.xml")));
Result output = new StreamResult(new File("abc.xml"));
Source input = new DOMSource(doc);
// xml processing instruction and comment node
ProcessingInstruction xmlpi = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"");
Comment comment = doc.createComment("DO NOT EDIT THIS FILE");
// first transform the processing instruction and comment
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(xmlpi), output);
transformer.transform(new DOMSource(comment), output);
// then the document
transformer.transform(input, output);
关于这一点,存在着一种争议。它不是固定的(正如您所期望的),因为这可能会给用户的现有应用程序带来许多问题
添加以下输出属性可修复此问题:
transformer.setOutputProperty("http://www.oracle.com/xml/is-standalone", "yes");
有同样的问题。
我通过将注释放在根元素中解决了这个问题。
不完全一样,但我认为可以接受。“我正在尝试在XML文档根节点上方的注释后添加换行符。”-你到底为什么要这样做?(不要回答,这是一个修辞性问题。我理解这纯粹是出于表面原因。我也不是说这是不可能的。)@tomalak感谢你的回答。这是为了确保文件在发布之前外观良好且可读。我将看到以下错误_org.w3c.dom.DOMException:HIERARCHY\u REQUEST\u ERR:尝试在不允许的位置插入节点。\u何时使用以下代码:doc.insertBefore(doc.createTextNode(“\\n”),docElem)代码>@Murali请参见修改后的答案。
StreamResult streamResult = new StreamResult(writer);
// output XML declaration with an empty document
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.transform(new DOMSource(), streamResult);
// output the document without XML declaration
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(doc), streamResult);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new FileInputStream(new File("abc.xml")));
Result output = new StreamResult(new File("abc.xml"));
Source input = new DOMSource(doc);
// xml processing instruction and comment node
ProcessingInstruction xmlpi = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"");
Comment comment = doc.createComment("DO NOT EDIT THIS FILE");
// first transform the processing instruction and comment
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(xmlpi), output);
transformer.transform(new DOMSource(comment), output);
// then the document
transformer.transform(input, output);
transformer.setOutputProperty("http://www.oracle.com/xml/is-standalone", "yes");