Developer 工具已更新 1 天前

XML Beautifier

XML Beautifier reformats XML with clean two-space indentation, one element per line and a proper XML declaration. Comments, CDATA, DOCTYPE and namespaces are kept, while whitespace and attribute quoting are normalised. Input must be well-formed XML. Copy the result or download it as a .xml file.

永久免费 易于使用 立竿见影的效果 私密且安全

使用 XML Beautifier

粘贴代码文件的直接链接,将其内容加载到编辑器中。

输出

完成

阅读内容

了解该工具的工作原理以及何时使用它。

What this XML beautifier does

Paste an XML document and the tool parses it, then writes it back out with consistent indentation and line breaks. It is a real parse-and-reserialise step, not a search-and-replace: the file must be well-formed XML before anything is formatted, and the output is generated from the parsed tree rather than from the original characters.

That approach gives predictable results. Elements that contain only other elements are placed on separate lines and indented by two spaces. Elements that contain text keep their content inline, because breaking text across lines would change what the document says. Empty elements are collapsed, the XML declaration is ensured, and attributes are written with double quotes.

Before and after

Input:

<root><p>Hello <b>bold</b> <i>it</i></p><empty></empty><c><![CDATA[a < b]]></c><!-- note --><x a="1" /></root>

Output:

<?xml version="1.0"?>
<root>
  <p>Hello <b>bold</b> <i>it</i></p>
  <empty/>
  <c><![CDATA[a < b]]></c>
  <!-- note -->
  <x a="1"/>
</root>

Note that the paragraph stays on one line. Mixed content, where text and elements share a parent, is deliberately left alone so the sentence still reads correctly.

What is preserved

ItemBehaviour
CommentsKept, and placed on their own indented line where possible
CDATA sectionsKept as CDATA; the contents are not escaped or altered
DOCTYPE declarationKept, including a SYSTEM identifier
Namespaces and prefixesKept as written
Attribute valuesKept, with quotes normalised to double quotes
Attribute orderNormally preserved as parsed
Text whitespaceKept exactly, including runs of spaces
Empty elementsRewritten as self-closing tags such as <empty/>

What changes

  • Indentation becomes two spaces per nesting level, replacing whatever the file used before.
  • Line breaks are inserted between sibling elements where the parent holds only elements.
  • The XML declaration is added if the input has none; an existing declaration is kept.
  • Whitespace-only text nodes between elements are consumed and replaced by the new indentation, so the visual layout of the source is not preserved.
  • Attribute quoting is normalised, so single-quoted attributes come back double-quoted.
  • Entities and escapes are written in the canonical form produced by the XML writer, which may differ from how they were typed (for example a numeric escape for the same character).

None of these changes affect the document's meaning. They do affect diffs: running the formatter over a file in version control will rewrite every line that gained indentation.

Encoding and entities

The writer follows the encoding named in the XML declaration. A UTF-8 document stays UTF-8, and accented or non-Latin characters are normally written as themselves rather than as escapes. If the declared encoding cannot represent a character, the writer falls back to a numeric character reference so the document remains valid. A document that declares no encoding is treated as UTF-8 and gains the plain declaration line described above.

Entity references, written as an ampersand plus a name or number and a closing semicolon, are handled by the parser. The five predefined XML entities are always safe. A named entity defined only in an internal DTD subset depends on how the parser resolves it, and an entity defined in an external DTD is not fetched at all, which can leave a reference unresolved or fail the parse. If a document relies on a custom entity set, check the output before using it.

Namespaces are not rewritten. A default namespace stays default, prefixes stay as written, and no prefix is invented or removed. That matters for documents that mix vocabularies, such as an SVG fragment inside an XHTML wrapper.

Well-formedness is mandatory

The parser is strict. A tag that is not closed, an unquoted attribute, two root elements, a stray ampersand, a mismatched case in a tag name or an undeclared namespace prefix all cause the parse to fail. When that happens the tool returns an error and produces no output at all, not even a partially formatted document.

This is why HTML usually fails here. HTML tolerates unclosed <br> and <img> tags, optional end tags and bare ampersands; XML does not. If you need to tidy HTML, use an HTML formatter. If you need to turn HTML into XML, fix the structure first.

How to use the page

  1. Check that the language selector reads XML. The editor is shared with the other code tools, and the selected language decides which service runs.
  2. Paste the document into the Manual tab, or fetch a text file through the From URL tab.
  3. Press Beautify. The formatted document appears in the output panel.
  4. If an error appears instead, the input is not well-formed; fix the structure and try again. The error message is generic, so an XML validator with line numbers is more useful for large files.
  5. Use Copy or Download to take the result as beautify-output.xml.
  6. The footer badges report input and output character counts, which shows how much indentation was added.

Minify mode

The page also offers a Minify button. For XML it parses the document, disables pretty output and saves it without indentation, so the tree collapses onto fewer lines. It keeps the XML declaration, comments and CDATA sections; it is a whitespace removal pass, not a content compressor. If your goal is the smallest possible payload, a dedicated XML minifier that also strips comments will do more.

When it is useful

  • API payloads. A SOAP or REST response pasted from a log becomes readable in one step.
  • Configuration files. Maven, Ant, Spring, Android layout and many build files are XML and benefit from consistent indentation.
  • SVG source. A one-line exported SVG becomes an editable tree. Be careful with <text> and <tspan>: the formatter preserves text content, but reindenting can still change whitespace between elements in ways that affect rendering.
  • Code review. A formatted diff is easier to read than a single 4,000-character line.
  • Well-formedness check. If the tool rejects the file, it is not valid XML, which is itself a useful signal.

Limits and caveats

Input is capped at 200,000 characters per request, and the conversion runs on the server. The service does not fetch external DTDs, so a document that depends on an external entity definition may fail or keep the reference unresolved. Very deep nesting can hit parser limits. The formatter does not validate against a schema, does not check business rules, does not fix broken markup and does not reorder attributes or elements. It also cannot preserve your original line wrapping, because wrapping is exactly what it recomputes.

Privacy

Your XML is sent to the server for parsing. Remove credentials, tokens, personal data and internal endpoint names before pasting, especially from SOAP envelopes and configuration files. The tool does not store a document history or return anything except the formatted text.

Common mistakes

  • Pasting HTML. Optional end tags and bare ampersands fail the strict XML parser.
  • Expecting comments to be removed in Beautify. They are preserved; Minify keeps them too.
  • Expecting the original indentation to survive. Indentation is recalculated from scratch.
  • Assuming a failure means the tool is broken. It usually means the document is not well-formed.
  • Formatting a signed document. Any byte change invalidates XML signatures; format before signing, never after.
  • Pasting secrets. The request leaves your browser.

Frequently Asked Questions

常见问题解答

针对用户在信任结果之前常提出的疑问,提供简短解答。

It checks well-formedness, because it has to parse the document. It does not validate against a DTD or schema and does not check that element names or values are correct.

Yes. Comments, CDATA sections and the DOCTYPE declaration survive both Beautify and Minify. Only whitespace and indentation are recalculated.

HTML allows unclosed tags and bare ampersands; XML does not. Convert it to well-formed XHTML or use an HTML formatter instead.

The writer always emits one, so a document without a declaration gains the standard XML declaration line.

Yes. Formatting runs on the server, so the XML is sent with the request. Strip secrets and personal data first.

Not safely. Reformatted bytes no longer match the signature, so format before signing or leave signed files untouched.

200,000 characters per request. Larger documents are rejected.

评论 (0)

用于用户讨论、边界情况及后续建议。
发表评论
您的评论提交后将公开展示。
目前尚无评论。快来发表第一条评论吧!
请告诉我们!
报告此工具的问题

请描述您遇到的问题,以便我们进行调查并改进该工具。

什么最有帮助
请提供输入内容、预期结果、实际结果、浏览器/设备信息,如有帮助,请附上截图。
“截图”按钮会截取浏览器中的页面,并将生成的图片作为附件添加到此表单中。
整页截图
通过 newisty 在您的浏览器中捕获。
尚未截取屏幕截图。
工具 报告