Developer Tools

Formatting HTML and XML for Readability: A Developer Workflow

A practical workflow for turning one-line HTML and XML into readable, diff-friendly files, with worked examples and the cases where a simple formatter gets it wrong.

2026-11-138 min readBy ToolHive Team

Why format markup at all

Most HTML and XML you actually have to debug arrives as one long line: a SOAP response copied from a log, a template exported from a CMS, a sitemap fetched with curl, the "view source" of a page that ships minified. You cannot see the nesting, so you cannot see the mistake. Re-indenting the text is the cheapest first step in almost every debugging session, and it takes seconds.

This guide covers a simple workflow for doing that, what to look for once the markup is readable, and the specific situations where a lightweight formatter misleads you. It is written for the formatters on this site, so the limits described are the real limits of those tools.

What a text-based formatter does (and does not)

The HTML formatter and the XML formatter work on text, not on a parsed document. Each one splits the input wherever one tag ends and the next begins, then walks the lines from top to bottom keeping a depth counter:

  • A closing tag (a line starting with </) lowers the depth by one before the line is printed.
  • An opening tag that is not closed on the same line raises the depth for the lines after it.
  • Each line is printed with two spaces of indentation per level.
  • An element that opens and closes on one line, such as <p>Hello</p>, stays on one line and does not change the depth.

The XML formatter additionally leaves processing instructions (<?xml ... ?>), comments and self-closing tags alone when counting depth. The HTML formatter leaves comments and the doctype alone.

The important consequence: these tools never decide whether your markup is valid. They only follow the tag text you gave them. Garbage in produces tidy-looking garbage out. That is both the limit and, as the examples below show, a useful diagnostic.

Worked example 1: a one-line HTML snippet

Start with this input:

<div class="card"><h2>Title</h2><p>Hello</p><ul><li>One</li><li>Two</li></ul></div>

The formatter produces eight lines of output in this shape: the div sits at the left margin; h2, p and ul are indented one level (two spaces); the two li elements are indented two levels (four spaces) under the ul; and the closing </ul> steps back out to two spaces before the closing </div> returns to column zero. The text of each element stays inside its own line, so Title, Hello, One and Two remain attached to their tags.

Now you can see at a glance that the list belongs to the card and that nothing is unexpectedly outside it. That is all formatting gives you, and it is often enough.

Worked example 2: a compact XML response

Input (one line):

<?xml version="1.0"?><order id="7"><customer>Ana</customer><items><item sku="A1"/><item sku="B2"/></items></order>

Output: the XML declaration stays on its own line at column zero and does not deepen anything. order follows at column zero. customer (kept on one line with its text, Ana) and items are indented two spaces. The two self-closing item elements are indented four spaces under items, and because they are self-closing they do not push later lines deeper. </items> and </order> step back out correctly.

When two versions of an XML file are indented the same way, a line-by-line diff shows exactly which element changed, instead of flagging the whole single line as different. Format both before you compare.

The void-tag trap in HTML

HTML has elements that never take a closing tag: br, img, input, hr, meta, link and a few others. A parser knows this. A text-based formatter only sees an opening tag with no closing tag on its line and assumes more content is coming.

Take this input:

<div><p>Line one<br>line two</p><img src="a.png"><p>After</p></div>

The br is inside a line that also contains </p>, so it causes no trouble. But the standalone <img src="a.png"> line looks like an unclosed opener, so the formatter indents the following <p>After</p> one level too deep, and then the closing </div> lands one level too deep as well, so the closing tag ends up indented instead of at column zero.

Write the same input with the self-closing form, <img src="a.png" />, and the output is correct: div at zero, p, img and the second p all at two spaces, and the closing div back at zero. So the fix is mechanical: before formatting, add a trailing slash to void tags that stand alone between other tags.

This is also a good reminder that an odd indent is information. If the closing tag of your page is indented, the first place to look is just above the point where the indentation first drifted.

What mismatched tags look like

Because the formatter does not validate, broken XML is indented as written. Take <root><a><b>text</a></b></root>, where the closing tags are in the wrong order. The output does not warn you. Instead the lines after the mistake sit at inconsistent depths: the b line is indented like a sibling of a, and the trailing </b> pops out to column zero. Inconsistent indentation near a spot is a strong hint that tags are crossed there.

That hint is useful but not a verdict. In XML, a document with mismatched or unclosed tags is not well-formed, and a strict parser will refuse it outright. The formatter will not tell you that; a validator or the application that consumes the file will. Use the formatter to locate the problem, then fix it in your editor.

A repeatable workflow

  1. Copy a small, representative piece first. If a 2 MB file is slow to paste, take the section you care about. A fragment does not need to be a full document for the indentation to be useful.
  2. Normalise void tags (HTML only). Add the trailing slash to standalone img, input, hr and similar tags.
  3. Format. Use the HTML or XML formatter depending on the content. If the data is actually JSON, use the JSON formatter instead; it parses the text and reports syntax errors, which a tag formatter cannot.
  4. Read from the top. Find the first line whose indentation looks wrong. Everything above it is probably fine.
  5. Fix in your editor, not in the formatted copy. Treat the formatted output as a reading aid. Apply real changes to your source, then format again to confirm the structure is what you intended.
  6. Format both versions before diffing. This keeps the diff about content, not whitespace.

Format for reading, minify for shipping

Readable markup is for people. For delivery, extra whitespace is wasted bytes. The same idea applies across web assets: keep a readable source, serve a compact build. The CSS minifier does this for stylesheets, and the site also has minifiers for HTML and XML. Keep the formatted file as your source of truth and treat the minified one as generated output. Note that minifying is a one-way convenience: if you only keep the compact version, you will be back to the first section of this guide the next time you need to change it.

Limits: what these tools do not cover

  • No validation. Neither formatter reports unclosed tags, mismatched tags, invalid attributes or schema errors. A tidy result does not mean the document is correct.
  • Void elements in HTML. As shown above, standalone tags without a slash can push the rest of the output too deep.
  • Whitespace-sensitive content. Text inside pre or textarea, and mixed text and inline tags, can be re-broken. Do not paste the formatted output back if exact spacing matters. In some XML dialects whitespace in text is significant too.
  • Inline text with tags. A line like <p>Hello <b>world</b>, again</p> that begins and ends on one line is kept as it is, which is usually what you want, but the tool does not wrap long lines or re-flow paragraphs.
  • Empty elements. An element written as <empty></empty> is split onto two lines, with the closing tag on its own line, because the formatter splits wherever one tag meets the next. The data is the same; it just looks more verbose than the original.
  • Scripts and styles. The contents of script and style blocks are treated as text and are not formatted as JavaScript or CSS.
  • Comments and CDATA. They are placed on their own lines and indented to the current depth, but not otherwise processed.

Everything runs in your browser, so the text you paste is not uploaded by the tool. Even so, remove credentials, tokens and personal data from configuration files before you share formatted output in a ticket or chat.

Summary

Formatting is a quick way to make nesting visible. Use it to find where a structure goes wrong, to prepare files for a clean diff, and to read machine-generated output. Do not use it as a check of correctness. When something looks off, the likely causes in order are: a void tag without a slash (HTML), a missing or crossed closing tag, or text that really is whitespace-sensitive. If you can name which of those it is, you are most of the way to the fix.

Tools used in this article

  • HTML Formatter — Reformat HTML with proper nested indentation based on tag depth — a lightweight tag-based indenter, not a full HTML parser.
  • XML Formatter — Reformat XML with proper nested indentation based on tag depth — a lightweight tag-based indenter, not a full XML parser.
  • JSON Formatter — Pretty-print JSON with real 2-space indentation using the browser's native JSON parser — shows the exact parse error if your JSON is invalid.
  • CSS Minifier — Compress CSS by stripping comments, collapsing whitespace, and removing unnecessary spaces around punctuation.
#html#xml#formatting#debugging#developer workflow