How to format and check XML
- Paste your XML into Your XML, or press Open a file to load an .xml, .svg, .rss, .xsd, .config or other text file. You can also drag a file onto the box. Try an example fills it for you.
- The tool checks the text while you type. If it is well-formed, you see Well-formed XML, the formatted result on the right and a count of the elements, attributes and levels inside it.
- If something is wrong, the box on the right shows the lines around the mistake with a marker under the exact spot, and the message says what is wrong and how to fix it in plain words. Go to the mistake puts your cursor there.
- Choose how to see the result, then press Copy result or Download. Use result for my text moves the result back into the left box so you can keep editing it.
Formatted, minified, tree and JSON
- Formatted (pretty print) puts each child element on its own line, indented to show how the elements are nested. Choose 2, 3 or 4 spaces or a tab to match your project.
- Minified removes the line breaks and spaces between tags so the XML sits on one line. It is smaller and means the same to a program, which helps when you send it in a request or store it in a database field.
- Tree shows elements in folders you can open and close, with their attributes and text, which makes a long feed or SOAP response much easier to explore. The first two levels open for you.
- JSON converts the document for code that expects JSON: attributes get an @ in front of their name, repeated elements become lists, and text becomes the value.
- Keep comments is on by default. Turn it off to drop every comment from the result, for example before minifying a configuration file.
The most common XML mistakes
XML is strict: a program must refuse a document with even one mistake in it. These are the mistakes people make most often, and the tool names each one with its line and column:
- A tag that is never closed, such as
<item>without</item>. Every element needs a closing tag, or must be written in the empty form, such as<br/>. - Tags that cross, such as
<b><i>text</b></i>. The last element opened must be the first one closed. - A bare ampersand, such as
<company>AT&T</company>. Write&instead. The same goes for a<in text, which must be written<. - HTML entities such as
or©. XML itself knows only five:&,<,>,"and'. Use the character itself, or its number such as for a non-breaking space. - Attribute values without quotes, such as
<item id=7>. Writeid="7"orid='7'. - The same attribute twice in one tag, which XML does not allow.
- Two root elements. A document must have exactly one element at the top that holds everything else.
- Something before the declaration. If the document starts with
<?xml version="1.0"?>, nothing may come before it, not even a blank line. - Two hyphens inside a comment, such as
<!-- old -- new -->. Comments cannot contain--. - An undeclared namespace prefix, such as
<soap:Body>copied out of a larger message without itsxmlns:soapdeclaration.
Your text is kept exactly
Many formatters read the XML into a program and write it back out, and that round trip can quietly change it: entities are replaced by characters, attribute quotes change, CDATA sections turn into escaped text and spaces inside text are trimmed. This formatter copies every text, attribute value, comment, CDATA section and processing instruction exactly the way you wrote it, and only changes the spacing between tags. It also knows where spacing matters. In an element that mixes text and tags, such as <p>Press <b>Save</b> now</p>, the spaces around the tag are part of the text, so that element is left untouched; the same goes for anything inside an element marked xml:space="preserve".
Find parts of the document with XPath
XPath is a short language for pointing at parts of an XML document, used in XSLT, test tools, web scrapers and many programming libraries. Type an expression under Find with XPath and the matches are listed while you type. A few examples for the sample document:
//book/titlefinds every title of every book.//book[@lang='en']/titlefinds the titles of books whose lang attribute is en.//book[year > 1850]/authorcompares a child element's text with a number.count(//book)returns a number instead of a list, and//book[1]/@idreturns an attribute.//*[local-name()='title']matches by name whatever the namespace, which is the way to reach elements in a default namespace.
Where you meet XML
XML is still everywhere behind the scenes: SOAP web services, payment messages in the ISO 20022 standard, RSS and Atom news feeds, sitemaps for search engines, SVG images, Office documents (a .docx file is a zip of XML files), Android layouts, Maven and .NET project files, and GPX tracks from GPS devices. When one of those fails to load, a stray character or an unclosed tag is usually the cause, and formatting the file is the quickest way to see its structure and find the line.
Tips
- Compare two XML files by formatting both with the same indentation, then paste the results into the text compare tool. Only real changes will show.
- Working with JSON too? The JSON formatter checks and formats JSON the same way, and the CSV to JSON converter turns spreadsheets into JSON.
- Encoded data inside XML, such as a picture in a SOAP message, is often Base64; decode it with the Base64 tool.
- Keep secrets out of shared screenshots. The tool does not upload your XML, but anything you paste can still end up in a screenshot or screen share.
Your XML stays on your device
XML often holds private data, so everything happens in your browser: the checking, formatting, minifying, tree, JSON and XPath all run on your own computer or phone, and nothing is sent to this website or anyone else. The page remembers your latest text, XPath and settings in your browser on this device so they survive a reload. Clear removes the text, and clearing the site’s data in your browser removes everything.
Frequently asked questions
Is my XML uploaded to a server?
No. Checking, formatting, the tree, the JSON conversion and XPath all run in your browser, so nothing you paste or open is sent anywhere. That makes it safe for XML with private data, such as invoices, configuration files or SOAP messages. The page keeps your last text on this device so it is still there after a reload; press Clear to remove it.
What is the difference between well-formed and valid XML?
Well-formed XML follows the grammar every XML document must follow: one root element, every tag closed and nested correctly, attribute values in quotes, and special characters written with references. This tool checks exactly that. Valid XML is well-formed and also matches a schema (an XSD or DTD) that says which elements may appear where. Checking against a schema needs that schema file, which this page does not ask for.
Does formatting change my data?
No. Text, attribute values, comments and CDATA sections are copied character for character. The tool only adds or removes the line breaks and indentation between tags. Where spaces can matter, it leaves them alone: an element that mixes text with tags, such as a paragraph with a bold word, and any element marked xml:space="preserve", are kept exactly as written.
Why does it say a prefix is not declared?
A name such as soap:Envelope uses the namespace prefix soap, and XML namespaces require that prefix to be declared with an attribute such as xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" on that element or one around it. Web browsers and namespace-aware parsers such as expat refuse a document with an undeclared prefix, so the tool reports it with the line and column.
How does the XML to JSON conversion work?
Each element becomes a key. Attributes become keys that start with @, text becomes the value, or a #text key when the element also has attributes or child elements, and a child element that appears more than once becomes a list. Every value is text, because XML has no numbers or true and false; a program reading the JSON decides what each value means.
Which XPath can I use?
XPath 1.0, the version built into web browsers, with all its functions such as count(), contains() and local-name(). Prefixes declared in your document work in the expression. An element in a default namespace (declared with a plain xmlns="…") has no prefix to write, so match it with //*[local-name()="title"].
How big a file can it handle?
A file you open can be up to 5 MB, and pasting works for similar sizes. A few megabytes of XML is checked and formatted in well under a second on a normal computer. The tree shows the first 500 children of each element and XPath lists the first 100 matches, to keep the page quick; the text views show everything.