首页 全栈极客与开发辅助内容详情

XML Formatter & Validator: Errors With Line:Column, CDATA and Namespaces Intact - One-line mess in, indent tree out

2026-11-17 3 访客
XML Formatter & Validator: Errors With Line:Column, CDATA and Namespaces Intact

站点名称:XML Formatter & Validator: Errors With Line:Column, CDATA and Namespaces Intact

所属分类:全栈极客与开发辅助

官方网址:https://www.9314.cn/tools/xml-formatter/en/

SEO查询: 爱站网 站长工具

进入网站

站点介绍

XML Formatter & Validator: Errors With Line:Column, CDATA and Namespaces Intact

Debugging a third-party payment callback, the XML arrived as a single 20,000-plus-character line. Opening it in an editor meant counting nesting levels by cursor position until the eyes glazed - what finally cracked it was pasting the blob into a formatter and seeing the missing close tag at depth four. This page was built after that night: format, minify, errors with coordinates, one tool.

The short version

  • Format/minify one click apart: 2/4-space indent; CDATA blocks untouched, comments preserved verbatim, namespace prefixes kept, self-closing tags never expanded into pairs.
  • Errors with line:column: unclosed tags, unquoted attributes, bare ampersands, HTML entities smuggled into XML, mismatched closes, multiple roots - all reported as "line L, column C", not the browser's one-word parsererror. An own parser reports the errors; DOMParser cross-checks alongside, and both must pass before output.
  • Round-trip stable: format, minify, format again - the second output matches the first character for character. The reason is simple: minification strips only the indentation formatting introduced, never the content.
  • Browser-local processing, zero uploads; 1MB cap; nesting beyond 200 levels rejected (stack-overflow guard).

Why not just lean on DOMParser

The browser's native DOMParser parses XML fine, but its error report is one word: parsererror. Which line? Which column? What exactly? Nothing. For someone debugging, that is the same as silence.

So this page ships its own parser: character-by-character scanning, tokens for tags/attributes/text/comments/CDATA/PIs, pairing checks at the syntax-tree level. Errors convert positions into line:column coordinates - "3:15 attribute value missing quotes" is information; parsererror is not. DOMParser stays as a second-opinion cross-check, and both must pass before anything is emitted (edge-case insurance).

The entity trap most people step in

XML recognizes exactly five predefined entities: lt, gt, amp, apos, quot, plus numeric ones (&#...; / &#x...;).   and friends belong to HTML - in XML they are undefined entities, and plenty of tools pass them through silently until the browser chokes on open.

This page reports undefined entities with coordinates and tells you which entity it was and which ones XML actually accepts. Inside CDATA sections the rules relax - the whole block is text, preserved verbatim.

FAQ

How do I use the line:column coordinates in an error?

Just count along: lines start at 1 counted by newline characters, columns start at 1 counted by characters, pointing at the offending character. For example "3:15 attribute value missing quotes" means around column 15 of line 3 there is an unquoted attribute value. It is the same convention as our JSON tool's errors - fix it, press the button again, and it passes. The browser's native DOMParser only hands you a single parsererror word with no coordinates, which is exactly why this page uses its own parser.

Will my comments and CDATA survive?

Yes. Comments are preserved verbatim in formatted output (indented to their nesting level), and CDATA sections pass through untouched - the < and & inside them are never modified. Self-closing tags keep their self-closing form instead of being expanded into an open/close pair. Namespace prefixes (ns:tag, xmlns:ns=...) are treated as part of the plain name, so prefixes never get stripped.

Does format → minify → format drift the output?

No - the round trip is stable: format, minify, format again, and the second output matches the first character for character. The reason is that minification only strips the indentation and newlines that formatting introduced; it never touches tag internals or text content. Whitespace inside text nodes belongs to the content and is preserved as-is. Run three rounds on any input and see for yourself.

Can this work as an XML validator? How is it different from DOMParser?

Yes. The common errors are all covered: unclosed tags, mismatched close tags, unquoted attribute values, bare ampersands, undefined entities (&nbsp; and friends are HTML, illegal in XML), illegal name start characters, and misplaced XML declarations. The difference is error quality: DOMParser returns one parsererror string with no coordinates, this page gives line:column. The page also runs DOMParser as a cross-check in parallel - both must pass before output is produced.

Is my config file uploaded anywhere?

No. Formatting, minification and validation all happen inside this device's browser - there is no upload endpoint and it works offline. Only preferences like indent width live in this browser's localStorage; they do not follow you to another device, and "Clear my data" in the footer removes them.

中文版:https://www.9314.cn/tools/xml-formatter/

 暂无评论,快来抢沙发吧~

发布评论