Skip to content

XML URL encoding

Paste XML and get a URL-safe, percent-encoded version you can put in a link, a query parameter or a form field. Choose %20 or + for spaces. Switch to Decode to turn it back into XML.

< → %3C · > → %3E · / → %2F · " → %22 · space → %20

URL-encoded
%3Cnote%20priority%3D%22high%22%3E%0A%20%20%3Cto%3ETeam%3C%2Fto%3E%0A%20%20%3Cbody%3EReview%20the%20XML%20payload%3C%2Fbody%3E%0A%3C%2Fnote%3E

Encoded · 84 characters → 142 characters

Everything runs in your browser; nothing is uploaded.

How XML URL encoding works

URLs can only carry letters, digits and a few symbols safely. URL encoding, also called percent-encoding, replaces every other character with a % followed by two hexadecimal digits. XML is full of characters that need this treatment: the angle brackets of every tag, the slash in every end tag, the quotes and equals signs in attributes, and the spaces.

  1. Letters, digits and a few safe symbols such as - _ . ~ stay as they are, so tag names and text remain readable.
  2. Every other character is turned into its UTF-8 bytes.
  3. Each byte is written as % plus two hex digits: < is byte 3C, so it becomes %3C.

The result is one long string with no spaces or special characters, which a server decodes back into the original XML.

Common character mappings

How the characters found in XML are URL-encoded
CharacterEncodedWhere it appears in XML
<%3CStart of every tag
>%3EEnd of every tag
/%2FEnd tags and self-closing tags
"%22Attribute values
=%3DAttributes
?%3FThe <?xml ?> declaration
:%3ANamespace prefixes such as soap:
&%26Entities such as &amp;
space%20 or +Between attributes and in text
line break%0ABetween lines of indented XML
é%C3%A9Accented text (two UTF-8 bytes)

Worked example: XML in a query string

An API takes an XML message in a parameter called xml. The message is:

<note to="Ana">Hi & bye</note>

Step 1: Encode the special characters

< → %3C · space → %20 · = → %3D · " → %22 · > → %3E · & → %26 · / → %2F

Step 2: The encoded value

%3Cnote%20to%3D%22Ana%22%3EHi%20%26%20bye%3C%2Fnote%3E

Step 3: Add it to the URL as the parameter value

https://api.example.com/send?xml=%3Cnote%20to%3D%22Ana%22%3EHi%20%26%20bye%3C%2Fnote%3E

Encode only the value, not the whole URL. The ? and = around it must stay as they are, or the server cannot find the parameter. The & inside the XML has to be encoded as %26, otherwise the server would read it as the start of a second parameter.

URL encoding vs XML escaping

Both change special characters, but for different reasons, and they are easy to mix up. XML escaping keeps a document well-formed; URL encoding lets data travel safely inside a URL.

Differences between URL encoding and XML escaping
URL encodingXML escaping
< becomes%3C&lt;
& becomes%26&amp;
Spaces%20 or +unchanged
Used forURLs, query strings, form dataText and attributes inside XML

To send XML in a URL, URL-encode it and nothing else. You do not need to XML-escape it first; the receiving server decodes the URL encoding and gets your XML back exactly. If you need to escape characters inside the XML itself, use the XML escape tool.

URL-encoding XML in JavaScript, PHP, C#, Java and Python

encodeURIComponent(xmlString)                     // JavaScript
rawurlencode($xmlString);                         // PHP
System.Web.HttpUtility.UrlEncode(xmlString);      // C# / .NET
URLEncoder.encode(xmlString, "UTF-8");            // Java
urllib.parse.quote(xml_string, safe="")           # Python

They differ in one detail: how they write a space. JavaScript, PHP’s rawurlencode and Python’s quote use %20. C#’s HttpUtility.UrlEncode and Java’s URLEncoder use +, the style HTML forms use. Servers read both correctly in a query string; if the receiving side expects %20, pick it in the tool, or in C# use Uri.EscapeDataString() instead.

In JavaScript, use encodeURIComponent, not encodeURI. The second one is meant for whole URLs and leaves &, =, / and ? alone, which breaks a query string as soon as the XML contains them.

Frequently asked questions

Should I URL-encode the whole URL or just the XML?

Just the XML, as the value of its parameter. Encoding the whole URL would also encode the ? and = that the server needs to find the parameter.

Why is URL-encoded XML so much longer?

Each special character becomes three characters (< is %3C), and accented letters become six or more. Indented XML also has many spaces and line breaks to encode. Turn on Minify first in the tool to drop the indentation.

Is URL encoding the same as Base64?

No. URL encoding replaces only the unsafe characters, so the tag names stay readable. Base64 rewrites the whole document into letters and digits. Use the XML to Base64 converter if a system asks for Base64.

Does URL encoding hide or protect the XML?

No. Anyone can decode it instantly. It only makes the XML safe to carry inside a URL, so never put passwords or private data in a URL this way.

Sources