gpt4 book ai didi

java - 如何转义 XML 的字符串?

转载 作者:行者123 更新时间:2023-12-02 04:46:21 26 4
gpt4 key购买 nike

如何转义 XML 的字符串?

package test;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;

public class XmlEscapeTest {

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

String str = FileUtils.readFileToString(new File("Demo.txt"));
String results = StringEscapeUtils.escapeXml(str);
System.out.println(results);

}

}

Demo.txt文件包含。

<sometext>
Here is Demo "Lines" that I'd like to be "escaped" for XML
& here is some Examples: on Java. Thats?good programming Language.
</sometext>

最佳答案

您可以使用CDATA - block

<sometext><![CDATA[
Here is Demo "Lines" that I'd like to be "escaped" for XML
& here is some Examples: on Java. Thats?good programming Language.]]>
</sometext>

请参阅此处的更多帮助:http://www.w3schools.com/xml/xml_cdata.asp .

关于java - 如何转义 XML 的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29625393/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com