简体   繁体   English

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

[英]How to add line break into XML file with Java?

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.我试图添加带有换行符的文本节点,但它不起作用。

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/>

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.例如,假设您使用 xinclude 将 then 嵌入到文件的最终版本中。 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).如果你把它放在根元素中,它会(你可能应该改变它来说明不编辑这个项目元素的内容,而不是出于同样的原因说文件)。

There is no newLine after the comment since, even if you activated the indentation, the comment and the following node have depth 0.评论后没有换行,因为即使您激活了缩进,评论和后续节点的深度也为 0。

You can add a new line character in XML file by using: \n or:您可以使用以下命令在 XML 文件中添加换行符:\n 或:

<string name="yourString">new line starts now \n this is a new line</string>

Also, you should rethink the formatting of your text to not using the angle brackets.此外,您应该重新考虑文本的格式,不要使用尖括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM