The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Connections Settings

Inserting Records

Selecting Records


Converting Records

Upgrading Guide

XML conversion


class XMLConverter
    public function convert(iterable $records): DOMDocument
    public function fieldElement(string $node_name, string $fieldname_attribute_name = ''): self
    public function recordElement(string $node_name, string $record_offset_attribute_name = ''): self
    public function rootElement(string $node_name): self

XMLConverter converts a CSV records collection into a PHP DOMDocument.


Prior to converting your records collection into XML, you may wish to configure the element and its associated attribute names. To do so XMLConverter provides methods to setup theses settings.

Because we are building a DOMDocument object, the XMLConverter object throws DOMException insted of League\Csv\Exception.



public XMLConverter::rootElement(string $node_name): self

This method sets the XML root name.

The default root element name is csv.



public XMLConverter::recordElement(string $node_name, string $record_offset_attribute_name = ''): self

This method sets the XML record name and optionnally the attribute name for the record offset value if you want this information preserved.

The default record element name is row.

The default attribute name is the empty string.



public XMLConverter::fieldElement(string $node_name, string $fieldname_attribute_name = ''): self

This method sets the XML field name and optionnally the attribute name for the field name value.

The default field element name is cell.

The default attribute name is the empty string.



public XMLConverter::convert(iterable $records): DOMDocument

The XMLConverter::convert accepts an iterable which represents the records collection and returns a DOMDocument object.


use League\Csv\XMLConverter;
use League\Csv\Statement;
use League\Csv\Reader;

$csv = Reader::createFromPath('/path/to/prenoms.csv', 'r');

$stmt = (new Statement())
    ->where(function (array $record) {
        return 'Anaïs' === $record['prenoms'];

$converter = (new XMLConverter())
    ->recordElement('record', 'offset')
    ->fieldElement('field', 'name')

$records = $stmt->process($csv);

$dom = $converter->convert($records);
$dom->formatOutput = true;
$dom->encoding = 'iso-8859-15';

echo '<pre>', PHP_EOL;
echo htmlentities($dom->saveXML());
// <?xml version="1.0" encoding="iso-8859-15"?>
// <csv>
//   <record offset="71">
//     <field name="prenoms">Anaïs</field>
//     <field name="nombre">137</field>
//     <field name="sexe">F</field>
//     <field name="annee">2004</field>
//   </record>
//   <record offset="1099">
//     <field name="prenoms">Anaïs</field>
//     <field name="nombre">124</field>
//     <field name="sexe">F</field>
//     <field name="annee">2005</field>
//   </record>
// </csv>

If needed you can use the CharsetConverter object to correctly encode your CSV records before conversion.