简体   繁体   English

使用带有换行符的Java使用XML写入xml文件?

[英]Writing to xml file using Java with symbols for line break?

Basically, I am trying to add in a line break from code to an xml file. 基本上,我试图将代码中的换行符添加到xml文件中。 If i use \\n it will throw an error. 如果我使用\\ n它将抛出错误。 I tried using <br /> or &#xA; 我尝试使用<br />&#xA; but to no avail. 但无济于事。

I want to write: "hello \\n HI!" 我想写:“你好,你好!” into a XML tag which becomes: 变成一个XML标签,该标签变成:

<tag>Hello
Hi</tag>

If you are writing to a file. 如果要写入文件。 then you can use "\\r\\n" or System.getProperty( "line.separator" ) 那么您可以使用“ \\ r \\ n”或System.getProperty(“ line.separator”)

public static void main (String[] args) throws IOException {

        String xml= "<tag>Hello "+System.getProperty("line.separator")+" world!</tag>";

        File dest = new File("D:\\sample.xml");
        if(!dest.exists())dest.createNewFile();

        OutputStream os = new FileOutputStream(dest);

        os.write(xml.getBytes());

        os.close();
 }

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

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