Convert XML to string literals for your code
Unit tests, mock servers and API clients often need an XML payload inside source code. Pasting it between quotes breaks immediately: every " in an attribute ends the string, and every newline is a syntax error. This XML to string converter escapes the document into a literal you can paste straight into Java, C#, JavaScript, TypeScript or Python, or use as a JSON string value.
Example
<user id="7">
<!-- test -->
<name>O'Neil "Pat"</name>
<path>C:\temp</path>
</user>
With the defaults (single-line, double quotes, Minify first):
"<user id=\"7\"><name>O'Neil \"Pat\"</name><path>C:\\temp</path></user>"
With Minify first off and Output set to Concatenated lines:
"<user id=\"7\">\n" +
" <!-- test -->\n" +
" <name>O'Neil \"Pat\"</name>\n" +
" <path>C:\\temp</path>\n" +
"</user>"
What each option does
- Output: Single-line string – one literal; line breaks become
\n. - Output: Concatenated lines – one quoted piece per line joined with
+, which keeps long payloads readable in code review. - Quotes – double quotes escape
"as\"; single quotes escape'as\'and leave double quotes alone, which suits XML full of attribute quotes. - Minify first – removes indentation, line breaks and comments before escaping. Untick it to keep the document exactly as written.
In every mode, backslashes are doubled and tab characters become \t. Non-ASCII characters such as é are left as they are.
Language notes
- Java and C# – use double quotes. Concatenated lines compile as written. Modern alternatives are Java text blocks and C# raw string literals, which need no escaping at all.
- JavaScript and TypeScript – either quote style works; single quotes give the cleanest result for attribute-heavy XML.
- Python – both quote styles work. For concatenated lines, wrap the whole expression in parentheses, because a line ending in
+is not a continuation on its own. - JSON – use double quotes and single-line output; JSON strings cannot use single quotes or
+.
Text blocks and raw strings still need care
Newer language features reduce escaping, but they do not all remove it. Take the example document again:
String xml = """
<user id="7">
<name>O'Neil "Pat"</name>
<path>C:\\temp</path>
</user>
""";
In a Java text block the quotes are fine, but escape sequences are still processed, so C:\temp must be written with a doubled backslash; a single one would turn \t into a tab. Python's triple-quoted strings behave the same way unless you add the r prefix. C# raw string literals ("""…""") are the exception and take the XML completely as written. When in doubt, the escaped output from this tool is the form that works in every version.
String versus entity escaping
This tool escapes for a programming language, not for XML. If you need to put markup inside another XML document, where < must become <, use XML escape instead. To only compact the document without quoting it, use XML minify. The same job for JSON payloads is done by JSON stringify.
Convert XML to a string in Python
To turn an ElementTree document into a string in Python, call ET.tostring() with encoding="unicode". Without that argument the function returns bytes, which is the usual surprise: joining the result to text raises TypeError, and printing it shows a b'...' wrapper. When you need the XML as an escaped literal for source code or a JSON value, json.dumps() adds the quotes and backslashes, and ensure_ascii=False keeps accented characters readable.
import json
import xml.etree.ElementTree as ET
root = ET.Element("user", id="7")
ET.SubElement(root, "name").text = "O'Neil"
ET.SubElement(root, "path").text = r"C:\temp"
as_bytes = ET.tostring(root) # default: bytes
as_text = ET.tostring(root, encoding="unicode") # str
print(type(as_bytes).__name__, type(as_text).__name__)
print(as_text)
# An escaped literal for source code or a JSON value
print(json.dumps(as_text, ensure_ascii=False))
The last line it prints is identical to what the converter above returns for the same document with the default settings, which is the quicker route when the XML is already on your clipboard.