How do I escape a String for 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);
}
}
and Demo.txt File Contains.
<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>
You could use a 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>
See here more help for this: http://www.w3schools.com/xml/xml_cdata.asp.