如何转义 XML 的字符串?
How do I escape a String for XML?
如何转义 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>
只有五个:
" "
' '
< <
> >
& &
它们很容易记住。 HTML 有自己的一组 escape codes,涵盖了更多的字符。
您可以使用 CDATA - 块
<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。
如何转义 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>
只有五个:
" "
' '
< <
> >
& &
它们很容易记住。 HTML 有自己的一组 escape codes,涵盖了更多的字符。
您可以使用 CDATA - 块
<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。