Parse XML into CSV
What parsing XML into CSV changes, when flattening a tree into rows works, what gets lost, and how to keep the CSV readable in spreadsheets and other tools.
What converting XML to CSV involves
XML stores data as a tree of nested elements and attributes. CSV stores data as a flat grid of rows and columns, one record per line. To convert one to the other, an XML parser reads the document. You then choose which repeating element becomes a row and map its child elements and attributes to columns. The tool might be a programming library, a command-line utility, a spreadsheet import feature or an online converter. All of them have to make this mapping decision, either automatically or with your input.
This is a change to the stored data, not to how it looks. The CSV file contains different characters arranged in a different structure from the source. It is not a restyled view of the original text. Anything the mapping leaves out is absent from the output file.
Is XML-to-CSV conversion suitable for your use?
Converting is a good choice when the XML is record-oriented and you need the data in a spreadsheet, a database import or another tool that expects tabular input. Record-oriented means a list of repeated elements with consistent, mostly single-valued fields. The main benefit is reach: almost any data tool can open CSV, and it is easy to scan, filter and sort. The main tradeoff is that a flat grid cannot hold a tree. Nesting, repeated children, the difference between attributes and elements, namespaces and the order of mixed content are either lost or squeezed into column names and cell values by convention.
- Usually a good fit: exports, feeds or logs made of many similar records with flat or shallow fields
- Usually a good fit: one-off analysis where you only need a subset of fields
- Usually a good fit: loading records into a tool that accepts CSV but not XML
- Better left as XML or plain text: document-style content where text and inline markup are interleaved, such as paragraphs with emphasis or links
- Better left as XML or plain text: deeply nested data where the number of repeated children varies a lot between records
- Better left as XML or plain text: workflows that must convert back to the same XML, validate against a schema, or preserve signatures or exact formatting
- Better left as XML or plain text: cases where an exact, human-readable copy of the original matters more than tabular access
What the conversion actually changes
Row selection. You or the tool pick one element type, such as each order or each item, to become a row. A different choice gives a different table from the same file.
Nested and repeated elements. A child element that appears once can become one column, often named with a path such as order_item. A child that repeats has to be handled in one of these ways:
• One row per child, which repeats the parent's values on each row.
• All values joined into a single cell with a separator.
• Numbered columns such as item_1 and item_2.
• A second CSV file linked to the first by an identifier.
Each option changes how the data is counted and read.
Attributes, namespaces and text. Attributes usually become columns, and you may need a naming rule so they don't collide with element names. Namespace prefixes may be kept, removed or expanded, depending on the tool. Comments and processing instructions are typically dropped. Entity references are resolved to the characters they stand for. Surrounding whitespace may be trimmed.
Types and absence. CSV has no data types, so numbers, dates and booleans all become text. An empty element, an element with only whitespace and a missing element can all end up as the same empty cell, unless the mapping marks them differently.
The illustrative example below uses one row per item. The order fields repeat on each item row. An order with no items still gets a row, with empty item cells. The comma inside 'Notebook, lined' is inside a quoted field, so it is not read as a column separator.
<orders>
<order id="1001">
<customer>Ana</customer>
<item sku="A1">Pen</item>
<item sku="B2">Notebook, lined</item>
</order>
<order id="1002">
<customer>Ben</customer>
</order>
</orders>
order_id,customer,item_sku,item
1001,Ana,A1,Pen
1001,Ana,B2,"Notebook, lined"
1002,Ben,,
In this output, counting rows gives the number of items plus orders with no items. It does not give the number of orders. If you instead put one order per row and join the items into one cell, the separator you choose must not appear in the item names. Otherwise the joined value becomes ambiguous.
Use a real XML parser rather than text find-and-replace or regular expressions. Text patterns tend to break on these XML features:
• CDATA sections
• Escaped characters
• Self-closing tags
• Attributes in a different order
• Elements that span several lines
If the XML comes from an untrusted source, use a parser configured not to resolve external entities. External entity resolution is a well-known way to expose local files or trigger unwanted network requests.
Will the CSV stay readable and usable after sharing?
Encoding. Save the CSV as UTF-8 and tell recipients that you did. Depending on the application and how the file is opened, accented letters and other non-ASCII characters can display incorrectly. Open the file in the destination you actually use before assuming it will display correctly everywhere.
Delimiters and regional settings. Some regional settings expect a semicolon rather than a comma as the separator, so the same file may open as a single column on one machine and as a proper table on another. Fields that contain the delimiter, a double quote or a line break must be quoted, and quotes inside them doubled. Not every tool handles line breaks inside fields equally well.
Automatic reinterpretation. Spreadsheet applications may reformat cells on import, for example:
• Removing leading zeros from codes
• Rounding or converting long numeric identifiers
• Turning date-like strings into dates
If you then save from the spreadsheet, these changes are written back to the file. Import the affected columns as text, or keep the original CSV unedited.
Formula injection. When the source data is untrusted, a cell value beginning with =, +, - or @ may be treated as a formula when opened in a spreadsheet. Check or neutralize such values before you share the file.
Accessibility, search and editing. CSV is plain text, so it can be searched, compared line by line and edited in any text editor. A clear header row is the most useful aid for anyone reading the data, including people using screen readers. How a screen reader presents a CSV depends on the program it is opened in. No documentation or test results for specific assistive technologies were available for this review, so verify with the tools your readers use.
Keeping a plain-text equivalent. Keep the source XML alongside the CSV and write down the mapping: which element became the row, how repeated children were handled and how columns were named. That record lets others interpret the table correctly and lets you regenerate it if the data changes.
Checklist before you convert
- Decide which element represents one row and what a row count should mean
- Choose how to handle repeated children and confirm that any separators don't appear in the data
- Define names for attribute and nested-element columns
- Use a proper XML parser with external entity resolution disabled for untrusted input
- Export as UTF-8 with correct quoting
- Open the result in the actual destination and check encoding, delimiters, leading zeros and dates
- Keep the original XML and a short description of the mapping
Limits of this review
No tool-specific documentation, measurements or test results were supplied. The guidance above therefore describes general XML and CSV behavior, not the performance of any particular converter. Feature sets, default mappings and privacy or data-retention practices differ between tools and cannot be judged from how a tool looks or markets itself. Confirm them in each tool's own documentation and with a small test file before you convert important data.