如何使用Java将换行符添加到XML文件中?

时间:2021-09-16 08:00:13

How to add line break after the "do not edit this file" comment? I've tried to add textnode with line break but it doesn't work.

如何在“不编辑此文件”注释后添加换行符?我试图添加带有换行符的textnode,但它不起作用。

Code:

码:

import java.io.File;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Main {
    public static void main(String[] args) {
        try {
            final Document doc = DocumentBuilderFactory.newInstance().
                    newDocumentBuilder().newDocument();

            doc.appendChild(doc.createComment(" DO NOT EDIT THIS FILE "));

            final Element rootElement = doc.createElement("projects");
            doc.appendChild(rootElement);

            final Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            transformer.transform(new DOMSource(doc), new StreamResult(new File("C:/abc.xml")));
        } catch ( Exception e ) {
            e.printStackTrace();
        }
    }
}

Output:

输出:

<!-- DO NOT EDIT THIS FILE --><projects/>

如何使用Java将换行符添加到XML文件中?

2 个解决方案

#1


0  

I have a blank line as well when I ran your code. Are you viewing the file in an editor, or parsing and displaying it? I ask because xml:space="preserve" settings might be wonky in your parser code if that's the case.

当我运行你的代码时,我也有一个空白行。您是在编辑器中查看文件,还是解析并显示它?我问,因为如果是这种情况,你的解析器代码中的xml:space =“preserve”设置可能会有问题。

The other option is to put the comment within the XML root element itself:

另一种选择是将注释放在XML根元素本身中:

        final Element rootElement = doc.createElement("projects");
        doc.appendChild(rootElement);
        rootElement.appendChild(doc.createComment(" DO NOT EDIT THIS FILE "));

In several ways this is more interoperable. For example let's say you use xinclude to embed then in the final version of the file. The way you have in your question the notice to not edit the file will not be included. If you put it within the root element it will (and you should probably change it to say something about not editing the contents of this project element, rather than saying file for the same reason).

在几个方面,这更具互操作性。例如,假设您使用xinclude将其嵌入到文件的最终版本中。您在问题中的方式将不包括不编辑文件的通知。如果你把它放在根元素中它(它应该改变它来说明不编辑这个项目元素的内容,而不是出于同样的原因说文件)。

#2


0  

There is no newLine after the comment since, even if you activated the indentation, the comment and the following node have depth 0.

注释后没有newLine,因为即使您激活了缩进,注释和以下节点的深度为0。

#1


0  

I have a blank line as well when I ran your code. Are you viewing the file in an editor, or parsing and displaying it? I ask because xml:space="preserve" settings might be wonky in your parser code if that's the case.

当我运行你的代码时,我也有一个空白行。您是在编辑器中查看文件,还是解析并显示它?我问,因为如果是这种情况,你的解析器代码中的xml:space =“preserve”设置可能会有问题。

The other option is to put the comment within the XML root element itself:

另一种选择是将注释放在XML根元素本身中:

        final Element rootElement = doc.createElement("projects");
        doc.appendChild(rootElement);
        rootElement.appendChild(doc.createComment(" DO NOT EDIT THIS FILE "));

In several ways this is more interoperable. For example let's say you use xinclude to embed then in the final version of the file. The way you have in your question the notice to not edit the file will not be included. If you put it within the root element it will (and you should probably change it to say something about not editing the contents of this project element, rather than saying file for the same reason).

在几个方面,这更具互操作性。例如,假设您使用xinclude将其嵌入到文件的最终版本中。您在问题中的方式将不包括不编辑文件的通知。如果你把它放在根元素中它(它应该改变它来说明不编辑这个项目元素的内容,而不是出于同样的原因说文件)。

#2


0  

There is no newLine after the comment since, even if you activated the indentation, the comment and the following node have depth 0.

注释后没有newLine,因为即使您激活了缩进,注释和以下节点的深度为0。