FormatterHQ

XML Escape

Make any text safe to place inside an XML element or attribute by replacing the five reserved characters with the predefined XML entities.

Loading tool…
Ready.

How to use the XML Escape

  1. Type or paste into the Raw text box, or click Sample. You can also drop a text file on either box, or load one into the Escaped XML box with its Open file button.
  2. The Escaped XML box updates live as you type. It works both ways — edit the Escaped XML side to convert back, or click ⇄ Swap to exchange the sides.
  3. Use Copy or Download on either box to take the result, or Clear to start again.

XML escape for elements and attributes

XML has exactly five predefined entities, and an XML escape replaces the matching characters with them. Without escaping, a single ampersand in a product name or a < in a formula makes the whole document fail to parse — XML parsers are strict and stop at the first error, unlike browsers reading HTML.

CharacterEntityRequired where
&&amp;Always
<&lt;Always
>&gt;Only in the sequence ]]>, escaped for safety
"&quot;Inside double-quoted attributes
'&apos;Inside single-quoted attributes

This tool always escapes all five, so the output is safe in text nodes and in attributes regardless of which quote style the document uses. Unlike the HTML escaper, it uses &apos; for the apostrophe, which is a native XML entity.

Example

Input:

<price currency="EUR">5 & 6 'ok'</price>

Output:

&lt;price currency=&quot;EUR&quot;&gt;5 &amp; 6 &apos;ok&apos;&lt;/price&gt;

XML escaping vs. CDATA sections

For long blocks of text that contain many special characters — embedded HTML, SQL or code — you can wrap the content in a CDATA section instead: <![CDATA[ 5 < 6 & more ]]>. Inside CDATA nothing needs escaping except the terminator ]]> itself. Escaping is the better choice for short values and attributes, because attributes cannot contain CDATA.

Characters XML cannot contain at all

XML 1.0 forbids most control characters (U+0000 to U+001F, except tab, line feed and carriage return), even as numeric references like &#1;. Escaping does not fix these; remove them before building the document. This tool leaves non-ASCII characters such as £ or é unchanged, which is correct for UTF-8 documents.

Other entities you may know from HTML, like &nbsp; or &copy;, are not defined in XML unless a DTD declares them. Use the literal character or a numeric reference (&#160;) instead.

Escaping XML in code

Most languages have a helper, but they do not all escape the same set. Python’s xml.sax.saxutils.escape() handles only &, < and > unless you pass extra entities, and quoteattr() is meant for attribute values. PHP needs htmlspecialchars($s, ENT_XML1 | ENT_QUOTES) to produce &apos;. In .NET, SecurityElement.Escape() covers all five. Building documents with a DOM or XML writer API is safer still, because it escapes values for you.

Related tools

Decode entities with XML Unescape, tidy the finished document with the XML Formatter, or check it parses with the XML Validator. The escaping runs in your browser, so nothing is uploaded.

Frequently asked questions

Which characters must be escaped in XML?
The ampersand (&) and less-than sign (<) must always be escaped in text. Quotes must be escaped inside attributes that use the same quote character, and > must be escaped when it follows ]]. Escaping all five is the simplest safe rule.
Is &apos; valid in XML?
Yes. &apos; is one of the five entities predefined by the XML 1.0 specification, together with &amp;, &lt;, &gt; and &quot;.
Why does my XML fail with “EntityRef: expecting ;”?
The document contains a bare & that is not the start of an entity, often in a URL query string or company name. Escape it as &amp;.
Can I use &nbsp; in XML?
Not unless a DTD defines it. Use the numeric reference &#160; or the literal non-breaking space character instead.

Last updated October 3, 2026

Related tools