Works on this device
XML Formatter & Validator
Paste XML to make it easy to read, to make it small, or to check it for mistakes. The tool shows the line and column of the first mistake. Your text is read in this browser and is not uploaded.
Your XML
Check these values
Your values are worked out in this browser. They are not sent to a server.
Your result will show here
Type or paste XML, or choose Try an example.
Result
How it was worked out
How to use this tool
- Type or paste your XML in the box, or choose Or open an XML file from your device. The result updates as you type.
- Choose what you want to do: format, minify, check, view as an outline, or view repeated elements as a table.
- To format, choose an indent of 2 spaces, 4 spaces or 1 tab. Tick Sort the attributes if you want the attributes of every tag in A to Z order.
- Choose Copy text or Download file to keep the result. Download CSV saves the counts and every row of a table.
What each option does
- Format (beautify) XML puts each element on its own line with indents. It also counts the elements and attributes and shows the size before and after in bytes.
- Minify XML removes the white space between tags. You can also remove the comments.
- Check if XML is well-formed says if the text follows the writing rules of XML 1.0. If it does not, you get the line, the column and a reason that says what is expected, and the line is shown with a mark under the place.
- View XML as an outline and a list of paths shows the elements, attributes and text as an outline, and lists each one with a path such as
/order/items/item[2]/@sku. - View repeated elements as a table makes one row for each element that repeats, such as each
itemof a list. Attributes and child elements become the columns.
Well-formed is not the same as valid
The word valid has two meanings for XML. Many people say valid when the text has no writing mistakes: every tag is closed, values are in quotes, and so on. The standard calls this well-formed, and this is what this tool checks.
In the standard, valid means more: the document also follows a DTD or an XML Schema (XSD) that says which elements and attributes are allowed. This tool does not do that check. It is a different and much larger job. A DTD or schema that a document names is never loaded here.
The XML rules that are checked
The tool has its own reader for XML 1.0 (Fifth Edition). It reads the text one character at a time and stops at the first place that breaks a rule.
- A document has one root element, and all other elements and all text are inside it.
- Every start tag has an end tag with the same name, in the same capital and small letters, and elements are closed in the reverse order of opening. An empty element can be written
<name/>. - An attribute is written
name="value"with the value in single or double quotes. A value cannot hold<. An attribute can be used once in a tag. - Names of elements and attributes start with a letter,
_or:and can hold letters, digits,-,.,_and:. &starts a reference. Without a declaration only<>&'and"exist, and character references such asAorA. A plain & is written&.- A comment is between
<!--and-->and cannot hold two hyphens next to each other. A CDATA section is between<![CDATA[and]]>. A processing instruction is between<?nameand?>. - The XML declaration, if there is one, is the very first thing:
<?xml version="1.0" encoding="UTF-8"?>. Its parts are version, then encoding, then standalone. - A DOCTYPE comes before the root element. Its declarations inside
[ ]are read: entities declared there are used, and the other declarations are read to their end and skipped. - Control characters are not allowed, apart from tab and line breaks. The three characters
]]>are not allowed in text. - With Also check the namespace rules ticked, every prefix such as
soap:must be declared withxmlns:soap="..."on the element or on an element around it, and a name can hold one colon only.
What happens to white space
Formatting and minifying change only white space that stands alone between two tags, with no other text beside it in the same element. The tool takes such white space as layout and writes its own.
- An element that holds text beside other elements, such as
<note>Leave it <b>at the door</b>, please.</note>, is left exactly as it is, with everything inside it. New line breaks there would change the text. - White space that is all an element holds, such as
<sep> </sep>, is kept. - Everything inside an element with
xml:space="preserve"is kept as it is. - Text, attribute values, comments, CDATA sections and references such as
&or are written back as you typed them. Spaces between the attributes of a tag become one space. - Line ends are written as line feeds. A program that reads XML treats all line ends the same, so this does not change the meaning.
- A program can treat white space between tags as data. If yours does, add
xml:space="preserve"to the element before you format.
Nothing is fetched or run
- The text is read as text only. No part of it is put into this page as HTML, and nothing in it is run.
- A DTD, entity, schema or stylesheet that the document keeps in another file or at a web address is not loaded. You get a warning that it was not read.
- An entity that is declared inside the document is used, with limits: entity references can add at most 1,000,000 characters, be used at most 100,000 times, and be open at most 40 deep inside each other. A document over these limits is refused with a message. Such a document can be made on purpose to overload a program.
Files and encodings
A file that you open is read on this device as UTF-8, or as UTF-16 when it starts with a byte order mark. A file in another encoding, such as ISO-8859-1 or Windows-1252, is not read: save it as UTF-8 first, or paste the text.
Text in the box has no encoding, so the encoding that the XML declaration names is checked for its spelling only. A file that you download from this page is saved as UTF-8. If the declaration names another encoding, change it in that file.
Worked example
The text <list><item>Pen</item><item>Ink</list> is not well-formed. The check gives Line 1, column 32: the end tag </list> comes before <item> is closed. Add </item> before it. <item> was opened at line 1, column 23.
The text <list><item>Pen</item><item>Ink & paper</item></list> is well-formed and is 57 bytes. Formatted with 2 spaces it has 4 lines and is 64 bytes: <list>, then the two items with two spaces before each, then </list>. The reference & is still &. The document has 3 elements and the deepest nesting is 2 levels.
The text <p:a>x</p:a> is well-formed XML, but the prefix p is not declared. With the namespace rules ticked the result is Well-formed XML, but a namespace rule is broken, with a warning at line 1, column 2.
Limits you should know
- Well-formed XML has no writing mistakes. It does not mean that the data in it is correct, complete, or allowed by a DTD or schema.
- The check stops at the first mistake. Correct it and run the check again to find the next one.
- The tool takes up to 5,000,000 characters, files up to 5 MB, and elements nested up to 200 levels deep. The list of paths and the table show up to 200,000 rows; the page shows the first 500 rows and the CSV file has them all. A table has at most 200 columns.
- The declarations
<!ELEMENT,<!ATTLISTand<!NOTATIONinside a DOCTYPE are read to their end and skipped. Their own grammar is not checked, and default values of attributes that a DTD gives are not added. - The document is read by the rules of XML 1.0. A document that says version 1.1 is read by the same rules, with a warning.
- The outline does not fold and has no colours. Paths use the names as they are written, with their prefixes.
- An XML file from an address on the web cannot be opened here. Copy the text and paste it, or save the file and open it.
- Digital signatures, XPath queries and XSLT are not part of this tool.