FormatterHQ

XML Parser

Parse XML into a JavaScript-style object and see exactly how attributes, text and repeated elements are represented, before you write the code that reads them.

Input XML
Output XML
Ready.

How to use the XML Parser

  1. Paste XML, upload a file, load a URL or click Sample.
  2. Set Attribute prefix in tree (default @) to match the convention your code uses.
  3. Click Parse. The Tree tab shows the parsed object; the status bar shows the element count and root.
  4. Switch to Code for the formatted XML, or click Format to re-indent it with your chosen Indent.

What an XML parser turns your document into

An XML parser reads markup and hands your program a data structure. In JavaScript that is usually a plain object, and the rules for how XML becomes an object decide how you write every line that reads it. This XML parser shows you that object as an expandable tree, so you can check the shape before writing response.order.item[0] and discovering it is not an array.

The parsing is done with fast-xml-parser, a widely used JavaScript library, configured to keep attributes.

The mapping rules, by example

<order id="42">
  <customer vip="yes">Ava</customer>
  <item sku="A1">Mouse</item>
  <item sku="B7">Pad</item>
  <empty/>
  <qty>007</qty>
</order>

parses to:

{
  "order": {
    "@id": "42",
    "customer": { "@vip": "yes", "#text": "Ava" },
    "item": [
      { "@sku": "A1", "#text": "Mouse" },
      { "@sku": "B7", "#text": "Pad" }
    ],
    "empty": "",
    "qty": "007"
  }
}
XMLParsed as
Attribute id="42"Key with the prefix: "@id": "42"
Element with only textA plain string value
Element with text and attributesObject with the text under #text
Element repeated under one parentAn array
Empty elementEmpty string ""
Numbers, booleansStrings, exactly as written ("007" keeps its zeros)
CDATA sectionIts content, merged into the text
Declaration, processing instructions, commentsLeft out

The attribute prefix option

Attribute prefix in tree controls how attribute keys are marked. The default @ matches many XML-to-JSON conventions. Enter _ and you get _id; enter @_ to mirror fast-xml-parser's own default. If you clear the field, attributes become ordinary keys and are indistinguishable from child elements, which is only safe when names never collide.

Pitfalls to plan for in code

  • One or many. A single <item> parses to an object, two parse to an array. Code that loops over order.item must handle both; most libraries offer an "always array" setting for known list elements.
  • Mixed content loses its order. <note>Leave at <b>door</b> ok</note> parses to {"b": "door", "#text": "Leave atok"}. Object-style parsing is meant for data, not prose; use a DOM parser for documents like XHTML.
  • Everything is a string. Convert numbers and booleans yourself, which also protects IDs and ZIP codes with leading zeros.
  • Namespace prefixes stay in the key. <soap:Body> becomes the key soap:Body.

The same result in your own code

If your project uses fast-xml-parser, these options produce a very similar object (this page additionally lists attributes before child elements):

import { XMLParser } from 'fast-xml-parser';

const parser = new XMLParser({
  ignoreAttributes: false,
  attributeNamePrefix: '@',
  parseTagValue: false,
  ignoreDeclaration: true,
});
const data = parser.parse(xmlText);

Parse errors

Only well-formed XML is parsed. Otherwise the status bar shows the reason and position, for example Attribute 'id' is without value for <b id=1>. The XML validator explains the common causes.

Next steps

To get the parsed structure as a JSON document you can save, use XML to JSON. To generate type definitions for it, try XML to TypeScript. If you only need a few values, an expression in the XPath tester is often simpler than walking the object.

Privacy

Parsing runs in your browser and your XML is not uploaded. Load URL fetches through our server, and Save & Share stores the input on our server for the link.

XML parser in Python

Python's standard XML parser is xml.etree.ElementTree. ET.fromstring() parses a string and ET.parse() reads a file; each element then exposes its tag, text and attributes, while find() and findall() select child elements by name. As in the tree on this page, every value arrives as a string, so 007 keeps its zeros until you convert it. The classic mistake is testing a lookup with if element:. find() returns None when nothing matches, but an element without children can also evaluate as false, so compare with is None.

import xml.etree.ElementTree as ET

xml_text = """<order id="42">
  <customer vip="yes">Ava</customer>
  <item sku="A1">Mouse</item>
  <item sku="B7">Pad</item>
  <qty>007</qty>
</order>"""

root = ET.fromstring(xml_text)
print(root.tag, root.attrib)                   # order {'id': '42'}
customer = root.find("customer")
print(customer.text, customer.get("vip"))      # Ava yes
print([(item.get("sku"), item.text) for item in root.findall("item")])
print(root.findtext("qty"))                    # 007, text is always a string

note = root.find("note")                       # no such element
print(note is None)                            # True

Paste your document into the parser above to see its structure before writing the lookups, or generate dataclasses for it with XML to Python.

Frequently asked questions

Why is an element sometimes an array and sometimes an object?
The parser only creates an array when an element name occurs more than once under the same parent. With a single occurrence you get an object or string. Normalize this in your code or use an always-array option for list elements.
Why are my numbers shown as strings?
Values are kept exactly as written in the XML, so 007 and 1.50 are not altered. Convert them in your code once you know which fields are numeric.
What does #text mean in the parsed tree?
#text holds the text content of an element that also has attributes or child elements. An element with only text is shown as a plain string instead.
Does the parser keep the XML declaration and comments?
No. The declaration, processing instructions such as xml-stylesheet, and comments are not part of the parsed data. They remain visible in the Code tab.
Can I use a different attribute prefix like @_ or $?
Yes. Type any prefix into the Attribute prefix in tree field. Clearing it removes the prefix entirely, which can cause attributes and child elements with the same name to clash.

Last updated October 3, 2026

Related tools

JSON Parser Parse JSON online: errors with line and column, the parsed values with their types, and the pitfalls of big integers and duplicate keys. YAML Parser Parse YAML online and see the resulting data as JSON and a tree. Check types, anchors and multi-document files. XML Pretty Print Pretty print XML in the browser, with matching commands for xmllint, xmlstarlet, Python and .NET when it belongs in a script. Sort XML Online Sort XML elements alphabetically by tag name or by an attribute value, at every level. Optionally sort attributes as well. XML to String Converter Convert XML to an escaped string literal for Java, C#, JavaScript or JSON. Minify first or keep lines as concatenated strings. XML Validator Validate XML online for well-formedness. Find unclosed tags, bad nesting and invalid characters with exact line and column numbers. XML Viewer View XML as a collapsible tree alongside formatted text. Inspect elements, attributes and values in any XML document. XML XSL Transform Apply an XSLT stylesheet to XML online and preview the result. Produce HTML, XML or text output from any XML document. XPath Tester Test XPath expressions online against your XML. See matched nodes, attributes, strings, numbers or booleans instantly. MXML Viewer View and format Adobe Flex / Apache Royale MXML files online with a component tree and neatly indented markup. OPML Viewer View OPML subscription lists and outlines as a clickable, nested list. Inspect feed URLs exported from any RSS reader. RSS Feed Viewer View RSS and Atom feeds as readable articles. Paste feed XML to preview titles, links, dates and summaries.