XML Character Escape
Convert special characters to XML entities for valid XML documents
About XML Character Escaping
XML (eXtensible Markup Language) is a markup language for storing and transporting data. Certain characters have special meanings in XML, and using them directly causes parsing errors, so they need to be escaped as entity references.
XML escaping ensures that data can be safely embedded in XML documents, regardless of what characters the data contains. This is crucial for generating valid XML output, processing user input, and data exchange.
XML Escape Rules
| Original Character | Entity Reference | Numeric Reference | Description |
|---|---|---|---|
| < | < | < | Less than |
| > | > | > | Greater than |
| & | & | & | Ampersand |
| " | " | " | Double quote |
| ' | ' | ' | Apostrophe |
Common Use Cases
XML Document Generation
Ensure text data is properly escaped when dynamically generating XML content.
SOAP Web Services
Special characters need to be escaped when included in SOAP messages.
Configuration Files
Handle values containing special characters when editing XML configuration files.
Data Export
Ensure data integrity when exporting data to XML format.
Usage Tips
- < and & must be escaped in all XML content
- > can sometimes be left unescaped, but it's recommended to always escape it
- Quotes in attribute values need to be escaped based on the quote type used
- CDATA sections can avoid escaping but cannot contain ]]>
- Numeric character references can represent any Unicode character
CDATA Sections
For text containing many special characters, CDATA sections can be used to avoid escaping. CDATA sections start with <![CDATA[ and end with ]]>, and content within is not processed by the XML parser.