Pick a Parser Style
XML parsers come in two families. DOM-style parsers load the whole document into a tree—simple and fine for small files. Streaming (SAX/StAX/iterparse) parsers emit events as they read—required for large feeds.
Never build a parser with regex. Quoting, CDATA, namespaces, and nested tags will break hand-rolled patterns.
| Style | Best for | Examples |
|---|---|---|
| DOM / tree | Small docs, random access | ElementTree, DOMParser, fast-xml-parser |
| Streaming | Large files, low memory | iterparse, sax, node streams |
| Schema-aware | Contract enforcement | lxml + XSD, xerces |
Step-by-Step: Parse XML in Python
ElementTree is in the standard library and covers most jobs.
import xml.etree.ElementTree as ET
root = ET.parse("orders.xml").getroot()
for item in root.findall(".//item"):
sku = item.get("sku")
name = (item.findtext("name") or "").strip()
print(sku, name)Streaming with iterparse
Keeps memory flat for large catalogs.
for _event, elem in ET.iterparse("big.xml", events=("end",)):
if elem.tag.endswith("item"):
process(elem)
elem.clear()Namespaces
Prefixed tags often appear as {namespace-uri}localname. Register namespaces or match on the Clark notation.
ns = {"p": "https://example.com/products"}
for item in root.findall(".//p:item", ns):
print(item.get("sku"))Step-by-Step: Parse XML in the Browser
DOMParser turns a string into a Document you can query.
const xml = await file.text();
const doc = new DOMParser().parseFromString(xml, "application/xml");
const parseError = doc.querySelector("parsererror");
if (parseError) throw new Error(parseError.textContent);
const items = [...doc.querySelectorAll("item")].map((el) => ({
sku: el.getAttribute("sku"),
name: el.querySelector("name")?.textContent?.trim() ?? "",
}));Step-by-Step: Parse XML in Node.js
fast-xml-parser is a popular choice for converting XML to plain objects.
import { XMLParser } from "fast-xml-parser";
import fs from "node:fs";
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: "",
});
const data = parser.parse(fs.readFileSync("orders.xml", "utf8"));
console.log(data);From Parsed XML to CSV or JSON
After parsing, most teams want a table or a JSON array. Map repeating elements to rows, attributes and children to columns, then write CSV—or skip the code for one-offs with the XML to CSV Converter on Convert CSV Online.
Security Notes
Untrusted XML can abuse entity expansion (XXE) and billion-laughs attacks. Disable external entity resolution unless you fully trust the source. Prefer hardened libraries and defaults that reject external DTDs.
Real-World Examples
Where parsing shows up daily.
Partner product feed
A nightly job streams a multi-million-line XML catalog, extracts SKUs and prices, and writes CSV for pricing review.
Browser upload preview
A SaaS importer parses a small XML upload in the browser, shows a table, then posts JSON to the API.
Support debugging
An engineer pastes a failing payload into a script, prints the first mismatched node, and opens a flattened CSV for the ops team.
Common Mistakes
These burn hours on otherwise simple jobs.
- Ignoring namespaces and getting empty NodeLists.
- Loading a huge file into a DOM when streaming would work.
- Treating textContent as trimmed when surrounding whitespace matters.
- Assuming attributes and child elements with the same name are interchangeable.
- Enabling external entities on untrusted input.
Best Practices
Keep parsing boring and safe.
- Validate well-formedness first, then parse.
- Handle namespaces explicitly.
- Stream large documents.
- Disable external entities by default.
- Convert to CSV/JSON only after you know the repeating row element.
Why Use Convert CSV Online?
When you only need a spreadsheet view, Convert CSV Online is free, browser-based, and requires no account for everyday conversions. Flatten with XML to CSV, edit in the Online CSV Editor, and skip writing a one-off parser. Client-side workflows work on Windows, macOS, and Linux browsers.
Prototype the mapping first
Confirm which element becomes a row online, then encode that mapping in Python or Node.
Conclusion
Parsing XML is choosing the right tool—tree for small docs, streams for large ones—then handling namespaces and attributes deliberately. Convert to CSV when humans need a table.
FAQ
How do I parse XML in Python?
Use xml.etree.ElementTree for most files, or ET.iterparse for large documents. Register namespaces when elements use prefixes.
How do I parse XML in JavaScript?
In the browser use DOMParser. In Node.js use a library such as fast-xml-parser. Always check for parsererror in the browser.
What is the difference between DOM and streaming parsers?
DOM loads the whole tree into memory. Streaming emits elements as they are read, which keeps memory low for large files.
Why does findall return nothing?
Usually a namespace mismatch. Match Clark notation {uri}local or pass a namespace map to findall.
Is it safe to parse untrusted XML?
Only with external entities disabled. Untrusted XML can trigger XXE and entity-expansion attacks.
How do I turn parsed XML into CSV?
Map repeating elements to rows and use a CSV writer—or use Convert CSV Online’s XML to CSV Converter for a quick flatten.
References
Convert your CSV in the browser
Preview, clean, and convert CSV files free with Convert CSV Online—no installation and no account required for everyday conversions.