Text to escape

Escaped text

Why escape text in XML?

Escaping XML is necessary when you want to include special characters in XML content that would otherwise be interpreted as part of the XML syntax. For example, the character "<" needs to be escaped as "&lt;" to prevent it from being interpreted as the start of an XML tag.

Examples of XML escaping

Original text

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <element attribute="value">
    <child>Text with special chars: & < > ' "</child>
    <data>CDATA sections like <![CDATA[ < > & ]]> don't need escaping</data>
  </element>
  <items>
    <item id="1">First item</item>
    <item id="2">Second & third</item>
  </items>
</root>

Escaped text

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;root&gt;
  &lt;element attribute=&quot;value&quot;&gt;
    &lt;child&gt;Text with special chars: &amp; &lt; &gt; &apos; &quot;&lt;/child&gt;
    &lt;data&gt;CDATA sections like &lt;![CDATA[ &lt; &gt; &amp; ]]&gt; don&apos;t need escaping&lt;/data&gt;
  &lt;/element&gt;
  &lt;items&gt;
    &lt;item id=&quot;1&quot;&gt;First item&lt;/item&gt;
    &lt;item id=&quot;2&quot;&gt;Second &amp; third&lt;/item&gt;
  &lt;/items&gt;
&lt;/root&gt;

XML Predefined Entities

CharacterEntityDescription
&&amp;Ampersand
<&lt;Less than
>&gt;Greater than
"&quot;Double quote
'&apos;Apostrophe (single quote)

When to use XML escaping

  • When creating XML documents programmatically
  • When inserting user-generated content into XML
  • When working with XML data that contains special characters
  • When generating XML for APIs, web services, or configuration files
  • When displaying XML code examples

XML vs HTML Escaping

While XML and HTML escaping are similar, there are some differences:

  • XML uses &apos; for apostrophes, while HTML often uses &#39;
  • XML is more strict about escaping characters
  • XML doesn't have as many named entities as HTML (like &nbsp;, &copy;, etc.)
  • CDATA sections in XML (<![CDATA[ ... ]]>) can be used to include text that shouldn't be parsed, avoiding the need for escaping

This tool is useful for preparing text to be safely included in XML documents, ensuring that special characters are properly escaped to maintain valid XML syntax.

Tip:

For large blocks of text that contain many special characters, consider using CDATA sections in your XML. Text within a CDATA section is not parsed by the XML parser, so special characters don't need to be escaped: <![CDATA[ your content here ]]>

Related Tools

All Tools

See all available tools