XML to String Converter
This XML to string converter turns a document into the escaped, single-line string literal you can paste into source code, a JSON field or a text database column. Double quotes are escaped so the attribute quotes in your XML do not end the string early, backslashes are doubled, line breaks become \n, tabs become \t, and remaining control characters become \u escapes. Because indentation between tags exists only for human readers, an option removes it before escaping, which typically halves the size of the literal while leaving the parsed XML identical. Escaping runs entirely in your browser.
How it works
Turns a document into the single-line string literal you can paste into source code, a JSON field or a text database column: double quotes are escaped, backslashes doubled, and line breaks become \n.
Removing whitespace between tags first keeps the literal short — useful when the XML is only ever going to be parsed, never read.
The conversion runs entirely in your browser — nothing you paste or upload is sent to a server.
How to convert XML to a string
- Paste your XML, or upload an
.xmlfile. - The escaped, single-line string literal appears on the right.
- Copy it and paste it wherever a string is expected.
What gets escaped
- Double quotes become
\", so the attribute quotes in your XML do not end the string early. - Backslashes become
\\. - Line breaks become
\nand tabs become\t. - Remaining control characters become
\u00XXescapes.
Note that this escapes XML for a string. It is not the same as XML entity encoding, which replaces
< and & with < and & so markup can sit
inside an XML text node — for that, use the HTML entities encoder.
Why collapse the whitespace
Indentation between tags is there for humans. Once the document is going to be parsed by a machine, every line break and run of spaces becomes an escape sequence that inflates the literal for no benefit. Removing whitespace between tags first typically halves the result, and the parsed XML is identical either way.
Common use cases
- Embedding a SOAP request or XML payload in a test fixture or code constant.
- Storing an XML document inside a JSON field or a text database column.
- Pasting XML into a shell script or an environment variable.
Frequently asked questions
What does converting XML to a string do?
It escapes every character that would break a string literal and puts the whole document on one line, so the XML can live inside a code constant, a JSON field or a text database column without terminating its container early.
Which characters get escaped?
Double quotes become an escaped quote, backslashes are doubled, line breaks become \n, tabs become \t, and any remaining control characters become \u00XX escapes.
Is this the same as XML entity encoding?
No. Entity encoding replaces characters like < and & with < and & so markup can sit inside an XML text node. This escapes the document for a string literal instead — different problem, different escaping. For entity encoding, use the HTML entities encoder.
Why remove whitespace between tags first?
Indentation is there for humans. Once a machine is going to parse the document, every line break and run of spaces becomes an escape sequence that inflates the literal for no benefit. Removing it typically halves the result, and the parsed XML is identical either way.
Should I keep the surrounding quotes?
Keep them when pasting somewhere that expects a complete string value. Turn them off when the destination already supplies its own quotes, such as between the quote marks of an existing string in code.
Is my XML sent anywhere when escaping?
No. Escaping happens entirely in your browser, so nothing you paste or upload is transmitted.
Related tools
JSON Stringify & Escape
This JSON stringify tool escapes a document into a string literal you can embed, and unescapes it again.
XML to JSON Converter
This XML to JSON converter parses XML into JSON online, preserving attributes and repeated tags as arrays.
HTML Entities Encoder / Decoder
Convert reserved HTML characters like < > & to entities, or decode named, numeric, and hex entities back to text.