# XML to CSV converter

Source: https://xmlvalid.com/tools/xml-to-csv

> Flatten XML into a table: pick the element that repeats (an invoice line, a product, a record) and every value under it becomes a column. Preview the table, then copy or download the CSV for a spreadsheet or a database. Everything runs in your browser.

## How the table is built

1. The document is read for elements that could be rows. Invoice lines come first (UBL `cac:InvoiceLine`, CII `ram:IncludedSupplyChainTradeLineItem`), then elements that repeat and hold other elements or attributes; choose another under **One row per**.
2. Every value under the row element becomes a column: child elements by their path (`cac:Item/cbc:Name`), attributes with `@` (`cbc:InvoicedQuantity/@unitCode`). Columns appear in the order first seen, and a row without a value gets an empty cell.
3. A value that occurs more than once in a row, such as two notes, is joined with `; `. **Short column names** drops the prefixes and keeps as much of the path as it takes to tell the columns apart.

## Opening the CSV in Excel

The CSV follows RFC 4180: fields with the delimiter, quotes or line breaks are quoted, and lines end with CRLF. Excel in many European locales expects semicolons, so choose **Semicolon** there, and tick **For Excel** before downloading: the file then starts with a byte-order mark, and accents and currency signs come through.

## E-invoice lines

For an XRechnung, Peppol or Factur-X invoice, the lines become a table ready for a spreadsheet. To read the whole invoice, open it in the [e-invoice viewer](https://xmlvalid.com/tools/e-invoice-viewer); to check it, in the [validator](https://xmlvalid.com/validate).

## Questions

### Is my XML uploaded?

No. The table is built in your browser.

### What happens to values outside the rows?

They are left out: the CSV holds what is inside the row element. Header data such as the invoice number belongs to every line; add it in the spreadsheet, or with an XSLT stylesheet in the XSLT transformer.

### Which delimiters can I use?

Comma, semicolon or tab.

### Can it convert CSV to XML?

No, this tool goes from XML to CSV only.

### Other tools

- [XML formatter](https://xmlvalid.com/tools/xml-formatter): Pretty-print or minify XML, keeping text, CDATA and comments as written.
- [XPath tester](https://xmlvalid.com/tools/xpath-tester): Evaluate XPath 3.1 against a document and see each match with its line.
- [XSD generator](https://xmlvalid.com/tools/xsd-generator): Infer an XML Schema from sample documents, checked against them.
- [XML from XSD](https://xmlvalid.com/tools/xsd-to-xml): Generate a sample XML document from a schema, validated against it.
- [XSLT transformer](https://xmlvalid.com/tools/xslt-transformer): Run XSLT 1.0, 2.0 or 3.0 on a document and see the result, the messages and the files it writes.
- [XML diff](https://xmlvalid.com/tools/xml-diff): Compare two XML documents as trees: changed, added and removed elements, attributes and text.
- [XML to JSON converter](https://xmlvalid.com/tools/xml-to-json): Convert XML to JSON, with arrays where an element repeats or wherever its XSD allows.
- [JSON to XML converter](https://xmlvalid.com/tools/json-to-xml): Convert JSON to XML: @keys become attributes, arrays repeated elements.
- [E-invoice viewer](https://xmlvalid.com/tools/e-invoice-viewer): Read an XRechnung, ZUGFeRD, Factur-X or Peppol invoice as a document, and print it.
- [XML validator](https://xmlvalid.com/validate): check a document against XSD, DTD, RelaxNG or Schematron, up to 1 GB.
