membersound
membersound

Reputation: 86847

How to save and encode a xml string with special characters?

I have a string with xml content. When I save it to File, the results contains tags like < " &gt

How can I save such a string correctly so that encoded characters are used?

Upvotes: 0

Views: 407

Answers (2)

David
David

Reputation: 20063

If you are determined to break the XML spec, then use...

http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html

The method "unescapeHtml" will do what you require.

Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes. Supports HTML 4.0 entities.

For example, the string "&lt;Fran&ccedil;ais&gt;" will become "<Français>"

If an entity is unrecognized, it is left alone, and inserted verbatim into the result string. e.g. "&gt;&zzzz;x" will become ">&zzzz;x".

Upvotes: 1

SLaks
SLaks

Reputation: 887767

You can't.
The XML specifcation says that <, &, and > must be escaped.

Upvotes: 1

Related Questions