0

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>
Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Duckkiee
  • 3
  • 1
  • 7
  • is there any alternative to format String gt, lt, quot, amp, and apos ? – Duckkiee Apr 14 '15 at 10:48
  • and i think alternative that &,'," " not good coding practice to use in Development... – Duckkiee Apr 14 '15 at 10:49
  • 2
    It's not clear what you're trying to achieve. Is your file meant to contain valid XML or not? What are you trying to do with it? – Jon Skeet Apr 14 '15 at 10:53
  • my file contain valid code,that program runs,, i just want to know is there any alternative like regex i.e define &, " " etc. escapeXml(str); in my java code, – Duckkiee Apr 14 '15 at 10:56
  • 1
    Your file doesn't contain *code* at all - it contains pseudo-XML, apparently. It's not clear what you want to do with that. If you want it to be the text node in some *actual* XML, I'd strongly recommend just using an XML API. – Jon Skeet Apr 14 '15 at 11:00
  • You can e.g. find the text between tags e.g. using ">(.*?)" regex and invoke `StringEscapeUtils.escapeXml(...)` on matching group. – Michal Kordas Apr 14 '15 at 11:02
  • http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/xml/XmlEscapers.html – NimChimpsky Apr 14 '15 at 11:41

2 Answers2

1

There are only five:

"   &quot;
'   &apos;
<   &lt;
>   &gt;
&   &amp;

They're easy to remember. HTML has its own set of escape codes which cover a lot more characters.

loshkin
  • 1,600
  • 2
  • 21
  • 38
1

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.

wumpz
  • 8,257
  • 3
  • 30
  • 25