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 "<" 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
<?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>XML Predefined Entities
| Character | Entity | Description |
|---|---|---|
| & | & | Ampersand |
| < | < | Less than |
| > | > | Greater than |
| " | " | Double quote |
| ' | ' | 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
'for apostrophes, while HTML often uses' - XML is more strict about escaping characters
- XML doesn't have as many named entities as HTML (like , ©, 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
- Unescape XML - Convert escaped XML entities back to their original characters
- Escape HTML - Escape text for use in HTML documents
- Escape JSON - Escape text for use in JSON strings
- Convert JSON to YAML - Convert between JSON and YAML formats
- Minify JSON - Remove whitespace and format JSON for production
All Tools
See all available tools