Format Code Now
/

XML Stringify Online

ED-50

Paste XML and click XML to string to get one escaped string literal that fits inside JSON or code. Click String to XML to unescape a literal back to XML text.

Runs in your browser. Nothing is uploaded.

XML or a string literal that holds XML

Result

How to use it

  1. Paste your XML into the input box.
  2. Pick a string style: double-quoted, single-quoted JavaScript, a template literal, or escaped without quotes.
  3. Click XML to string. Tick the minify option only if you want white space between tags removed.
  4. To reverse it, paste the string literal and click String to XML.

What stringify does to your XML

The tool first checks that the text starts like XML and that its tags balance. Then it escapes the whole text as one string literal: double quotes and backslashes get a backslash, and line breaks become \n, so the XML survives inside a JSON field or a line of source code. Entities such as & are left as written, because they are valid XML.

Minify and what it changes

The minify option removes the white space between tags and collapses runs of white space inside text, which makes the string shorter but changes text content. Leave it off when spacing matters. String to XML only unescapes the literal. It does not validate the result, parse it into a tree or convert it to JSON. Use the XML to JSON tool when you need that.

Error messages and what to do

MessageWhat it meansFix
This does not look like XMLThe text does not begin with a < character.Paste the XML starting at its first tag or declaration.
An XML document can have only one root elementThe input has more than one top-level element.Wrap the elements in a single root element.

Questions people ask

How do I convert XML to a string?

Paste the XML and click XML to string. The text is escaped so it can sit between quotes, with quotes and backslashes escaped and line breaks written as \n. Choose the style that matches where you will paste it.

How do I put XML inside a JSON value?

Use the double-quoted style. The output is a valid JSON string, so you can paste it as the value of a key. Choose the escaped style with no quotes if you are adding the quotes yourself.

Does stringifying change the XML?

Not unless you tick minify. Without it, every character is kept and only escaped. With minify on, white space between tags is removed and runs of white space inside text are collapsed, which can change content.

How do I turn the string back into XML?

Paste the string literal, with or without its quotes, and click String to XML. The escapes are removed and you get the XML text back. The tool does not check the result, so run it through a validator if you need to.

Why is my XML rejected?

The tool checks that the text starts with < and that the tags balance, and a document with two roots is reported. It does not check attributes against a schema. Fix the position named in the message and run it again.

About this tool

XML to string first checks that the text starts like XML and that its tags balance, then escapes it as one string literal: quotes and backslashes are escaped and line breaks become \n, so the XML survives inside JSON or source code. Entities such as &amp; are left as written. The minify option removes the white space between tags and collapses runs of white space inside text, which changes text content, so leave it off if spacing matters. String to XML unescapes a literal and returns the XML text without validating it. It does not parse the XML into a tree or convert it to JSON; use the XML to JSON tool for that. Nothing is uploaded.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.