Skip to content

XML escape and unescape

Escape <, >, & and quotes so XML treats them as text, or unescape &lt;, &amp; and other entities back into normal characters. Paste your text and the result appears as you type.

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

Escaped XML
Q&amp;A: is 5 &lt; 10? Yes, and 10 &gt; 5.

3 characters escaped · 32 characters → 42

Everything runs in your browser; nothing is uploaded.

What is XML escaping?

XML escaping replaces special characters with entity references, so an XML parser reads them as data instead of markup. A < normally starts a tag and an & starts an entity. When they are part of your text, you write them as &lt; and &amp; instead.

Without escaping, an ordinary sentence breaks the file:

<note>5 < 10 & 10 > 5</note>              ✗ parser error
<note>5 &lt; 10 &amp; 10 &gt; 5</note>    ✓ reads as "5 < 10 & 10 > 5"

Unescaping is the reverse: it turns &lt; back into <, so you can read text copied out of an XML file, a log or an API response.

XML special characters and entities

XML has five predefined entities. Each can also be written as a number, such as &#60; for <:

The five XML special characters, their entities and when to escape them
CharacterEntityNumberWhen to escape
& ampersand&amp;&#38;Always, in text and in attribute values
< less-than&lt;&#60;Always, in text and in attribute values
> greater-than&gt;&#62;Only in the sequence ]]>, but safe to escape everywhere
" double quote&quot;&#34;Inside attribute values wrapped in double quotes
' apostrophe&apos;&#39;Inside attribute values wrapped in single quotes

Escaping all five everywhere is still valid XML, and most tools do it to be safe. Choose Element text in the tool to escape only what text between tags needs, or Attribute value to escape the quotes as well.

Worked example: escaping text and attribute values

A menu item is called Tom's "Big" Fish & Chips. Where it goes decides what needs escaping.

Between tags (element text)

<name>Tom's "Big" Fish &amp; Chips</name>

Only the & changes. Quotes mean nothing between tags.

Inside an attribute

<item name="Tom&apos;s &quot;Big&quot; Fish &amp; Chips"/>

Now the quotes change too, or the first " would end the attribute early. Load the second example in the tool and switch between Element text and Attribute value to see both.

How to escape XML in code

Most languages have a built-in function for it:

# Python: escape() for element text, quoteattr() for attributes
from xml.sax.saxutils import escape, quoteattr
escape('5 < 10 & 10 > 5')          # '5 &lt; 10 &amp; 10 &gt; 5'

// C# / .NET: escapes all five
System.Security.SecurityElement.Escape(text);

// JavaScript: no built-in, so replace & first
text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;').replace(/'/g, '&apos;');

// PHP
htmlspecialchars($text, ENT_XML1 | ENT_QUOTES, 'UTF-8');

// Java (Apache Commons Text)
StringEscapeUtils.escapeXml10(text);

In JavaScript the order matters: if & is not replaced first, the & in each new &lt; gets escaped again. Better still, build the XML with a library such as Python’s ElementTree or .NET’s XElement and XmlWriter. They escape values for you, so you never do it by hand.

How to unescape XML

Switch the tool to Unescape and paste the escaped text. It turns the five entities back into characters, and also decodes numeric references in both forms: &#233; and &#xE9; are both é.

It also points out two common problems:

  • Text escaped twice. &amp;lt; only becomes &lt; after one pass. The tool spots this and offers to unescape again.
  • HTML entities. &nbsp;, &copy; and &mdash; work in HTML but not in XML, where a parser stops with an error such as “Entity 'nbsp' not defined”. The tool decodes the common ones and lists them, so you can replace them with numbers such as &#160;.

In code, Python’s xml.sax.saxutils.unescape() handles the basic entities, and C#’s System.Net.WebUtility.HtmlDecode() handles them all, numeric references included.

Frequently asked questions

Do I need to escape > in XML?

Only when it follows ]], because the sequence ]]> is not allowed in text. Everywhere else a raw > is valid, but escaping it as &gt; is always safe.

What does &amp;amp; mean?

The text was escaped twice: the first pass turned & into &amp;, and a second pass escaped that & again. Unescape it twice to get back to a single &.

Should I escape or use CDATA?

For short values and anything in an attribute, escape. For a long block of HTML, code or SQL between tags, a CDATA section (<![CDATA[ … ]]>) lets you paste it without escaping anything, as long as it does not contain ]]>.

Do I need to escape apostrophes in XML?

Only inside an attribute value wrapped in single quotes. In element text and in double-quoted attributes, ' can stay as it is.

Sources