The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Connections Settings

Inserting Records

Selecting Records

Interoperability

Converting Records

Upgrading Guide

This is the documentation for the upcoming version 9.0. This is a work in progress

XML conversion

<?php

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.

Settings

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.

XMLConverter::rootElement

<?php

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

This method sets the XML root name.

The default root element name is csv.

XMLConverter::recordElement

<?php

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.

XMLConverter::fieldElement

<?php

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.

Conversion

<?php

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

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

<?php

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

$csv = Reader::createFromPath('/path/to/prenoms.csv');
$csv->setDelimiter(';');
$csv->setHeaderOffset(0);

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

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

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

$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.