XML Formatter
Validate and pretty-print XML content
XML Formatter — what it does
Validate XML and pretty-print it with an indent size you choose, so nesting becomes visible instead of implied. XML that arrives as one long line is technically fine and practically unreadable, and formatting is usually the first step to understanding what a document contains.
Worked example
A one-line purchase order with attributes, nested elements, an empty element and a comment, pretty-printed with Indent size 2.
Input — XML (one line)
<?xml version="1.0" encoding="UTF-8"?><order id="A-1042" status="open"><customer><name>Ann Lee</name><email>ann.lee@example.com</email></customer><items><item sku="DESK-01" qty="2">Standing desk</item><item sku="ARM-04" qty="4">Monitor arm</item></items><notes></notes><!-- rush --></order>
Output — formatted
<?xml version="1.0" encoding="UTF-8"?>
<order id="A-1042" status="open">
<customer>
<name>Ann Lee</name>
<email>ann.lee@example.com</email>
</customer>
<items>
<item sku="DESK-01" qty="2">Standing desk</item>
<item sku="ARM-04" qty="4">Monitor arm</item>
</items>
<notes/>
<!-- rush -->
</order>The <?xml …?> declaration is kept as the first line, the empty <notes></notes> is collapsed to <notes/>, and the comment is kept.
When this tool helps
- Making an API response or configuration file readable while debugging.
- Finding the unclosed or mismatched tag that is stopping a document from parsing.
- Tidying XML before committing it or including it in documentation.
How to use it
- Paste the XML into the input box.
- Set the indent size.
- Process the document.
- Read the formatted output or the validation error.
Accuracy, limits, and good practice
- XML is strict where HTML is forgiving: every tag must be closed, nesting must match, and attribute values must be quoted.
- Whitespace between elements is technically content, so reformatting can matter for documents whose schema is whitespace-sensitive.
- Validation here checks that the document is well-formed; conformance to a schema or DTD is a separate question.
Frequently asked questions
What makes XML invalid?
Unclosed tags, mismatched nesting, unquoted attributes, multiple root elements, or unescaped ampersands and angle brackets in content.
Does it validate against a schema?
No, it checks well-formedness. Schema validation requires the XSD or DTD and a dedicated validator.
Should I use XML or JSON?
JSON for most modern APIs. XML remains standard in enterprise integrations, document formats and any workflow that needs schemas and namespaces.
Are comments and processing instructions before the root element kept?
Yes. The XML declaration is copied exactly as you wrote it (<?xml version="1.0"?> stays without an encoding), and a comment or a processing instruction such as <?xml-stylesheet type="text/xsl" href="order.xsl"?> placed before the root stays in the same order above it. A <!DOCTYPE> line is not printed.
How are entities, mixed content and CDATA handled?
Special characters are escaped again on output, so the result stays valid XML: <customer>Mark & Co <EU></customer> comes out unchanged, and an attribute note="Tom & Jerry <VIP>" is written as note="Tom & Jerry <VIP>" (a bare > is legal in an attribute). When text and child elements are mixed, each text run gets its own indented line and a CDATA section is kept as <![CDATA[x < y && z]]>. A parse error shows only the parser's message, for example "error on line 1 at column 25: Unexpected closing tag: order != item"; the output box keeps the previous result.
Related tools on this site
- YAML Formatter & Validator — Validate and consistently format YAML documents
- Fixed-width Text Wrapper — Wrap paragraphs to a chosen line width without breaking words
- Line Prefix & Suffix — Add numbering, prefixes, or suffixes to every line
Further reading: Cleaning Up Text You Pasted From Somewhere Else
Everything above runs inside this page. XML Formatter needs no account, no upload, and no server round trip — close the tab and nothing is left behind.
XML 格式化能做什麼
驗證 XML 並依你指定的縮排大小美化排版,讓巢狀結構從隱含變成看得見。擠成一行的 XML 技術上沒問題、實務上無法閱讀,而格式化通常是理解一份文件內容的第一步。
實際範例
一行式的採購單 XML,含屬性、巢狀元素、空元素與註解,以縮排 2 格的設定排版。
來源:XML(單行)
<?xml version="1.0" encoding="UTF-8"?><order id="A-1042" status="open"><customer><name>Ann Lee</name><email>ann.lee@example.com</email></customer><items><item sku="DESK-01" qty="2">Standing desk</item><item sku="ARM-04" qty="4">Monitor arm</item></items><notes></notes><!-- rush --></order>
排版後
<?xml version="1.0" encoding="UTF-8"?>
<order id="A-1042" status="open">
<customer>
<name>Ann Lee</name>
<email>ann.lee@example.com</email>
</customer>
<items>
<item sku="DESK-01" qty="2">Standing desk</item>
<item sku="ARM-04" qty="4">Monitor arm</item>
</items>
<notes/>
<!-- rush -->
</order><?xml …?> 宣告保留在第一行,空的 <notes></notes> 縮成 <notes/>,註解也會保留。
什麼時候用得上
- 除錯時讓 API 回應或設定檔變得可讀。
- 找出讓文件無法解析的未關閉或不對稱標籤。
- 在提交或放進文件之前先整理 XML。
使用步驟
- 把 XML 貼進輸入框。
- 設定縮排大小。
- 執行處理。
- 閱讀格式化結果或驗證錯誤。
精確度、限制與實務建議
- HTML 寬容、XML 嚴格:每個標籤都必須關閉、巢狀必須對稱、屬性值必須加引號。
- 元素之間的空白技術上也是內容,對空白敏感的結構描述而言,重排是有影響的。
- 此處的驗證檢查的是格式良好;是否符合 Schema 或 DTD 是另一回事。
常見問題
什麼情況會讓 XML 無效?
標籤未關閉、巢狀不對稱、屬性未加引號、有多個根元素,或內容中的與號與角括號未逸出。
會依 Schema 驗證嗎?
不會,它檢查的是格式良好。Schema 驗證需要 XSD 或 DTD 與專用驗證器。
該用 XML 還是 JSON?
多數現代 API 用 JSON。XML 在企業整合、文件格式,以及需要 Schema 與命名空間的流程中仍是標準。
根元素之前的註解與處理指令會保留嗎?
會。XML 宣告會照你寫的原樣保留(<?xml version="1.0"?> 不會被補上 encoding),根元素之前的註解或 <?xml-stylesheet type="text/xsl" href="order.xsl"?> 這類處理指令,也會依原本順序留在根元素上方。<!DOCTYPE> 那一行則不會輸出。
實體、混合內容與 CDATA 會怎麼排?
輸出時會重新跳脫特殊字元,結果仍是合法的 XML:<customer>Mark & Co <EU></customer> 原樣輸出,屬性 note="Tom & Jerry <VIP>" 會寫成 note="Tom & Jerry <VIP>"(屬性值裡單獨的 > 是合法的)。文字與子元素混在一起時,每段文字各自成為一個縮排的行,CDATA 區段也會保留為 <![CDATA[x < y && z]]>。解析錯誤時只顯示解析器的訊息,例如「error on line 1 at column 25: Unexpected closing tag: order != item」,輸出框則保留上一次的結果。
本站相關工具
- YAML 格式化與驗證 — 驗證 YAML 文件並統一重新排版
- 固定寬度文字換行 — 依指定字元寬度自動換行,並避免拆開單字
- 每行前後綴工具 — 為每一行加入編號、前綴或後綴
延伸閱讀: 清理從別處貼上的文字
以上步驟全部在這個頁面內完成,XML 格式化 不需要註冊、不上傳檔案、也不經過伺服器,關閉分頁後不會留下任何資料。