1. Start
  2. Introduction
  3. Quick Start
  4. Advanced features
  5. Class reference
  6. Live demo

Xml2Csv Converter PHP class

Xml2Csv is a helper class for easy XML to CSV conversion.


Xml2Csv Converter PHP class

This PHP class is an easy to use, highly customisable XML 2 CSV converter.
You can automatically convert XML to CSV, with only 1 parameter - the XML input (file or string).
Advanved features include custom selection of the output fields, setting CSV delimiter and enclosure character etc.
The output can be written returned as a string or saved to a file.



Example 1: Easy automatic conversion

This example shows, how easy you can convert a XML file to CSV


Example 2: Easy convertion from a string

In this example we use string input instead of a XML file

  $xml = "
  <?xml version="1.0" encoding="UTF-8"?>
     <Desc>Some item description</Desc>
      <Desc>Some other item description, which is much longer.</Desc>


Example 3: Changing convertion settings

It is possible to easily change the convertion settings.
This example shows, how to change the CSV delimiter to the ; character and the output type setting to "file".
We use the URL setting to indicate the XML file we want to use.
At last we call the autoConvert function to run the conversion.

  $x = new XmlToCsv();


Quick Start

To start converting, just  2 simple steps are needed.

1. Include the Xml2Csv PHP class in your code


2. Use the convert function to automatically convert your XML file


  // if your file is on the server, just use the path to the file

  // if you want to convert from a remote URL, use the whole URL


For more advanced examples refer to the Advanced features.















Advanced features

If the simple conversion is not enough for you, you can use the advanced features of the Xml2Csv class.
There are more ways how you can change the conversion settings.

Example 1: Chained parameter settings

You can use function chaining for setting the parameters.
Each paramter function is named as the parameter itself, so if you want to change the URL parameter, just call the url() function, if you want to change the ouput paramter, use the output() function. (for a complete function reference, checkout the function reference). First you have to create an object instance, and then set the parameters. At last, call the autoConvert() function to start the conversion.

    $x = new XmlToCsv();
    echo $x->url('example.xml')

Example 2: Set parameters separately by directly accessing the class variables

You can also set up the parameters by setting each variable separatly. Just create an instance of the class, and set each parameter separately. At last call the autoConvert() function to start the conversion. This example sets the same parameters as in Example 1.

    $x = new XmlToCsv();
    $x->url = 'example.xml';
    $x->output = false;
    echo $x->autoConvert();

Example 3: Set parameters at object creation using an array of options

The last way of setting parameters is at object creation. You can set all needed parameters by passing an array to the constructor. The parameters used are the same as in the examples above.

    $x = new XmlToCsv(array(
    echo $x->autoConvert();

Example 4: Combination of all 3 ways of parameter setting

Of course it is possible to combine all methods described above. In this example we have done the same settings as above, and moreover changed the CSV delimiter setting.

    $x = new XmlToCsv(array(
    $x->output = false;
    echo $x->delimiter(";")

For a list of all features and settings refer to the Class reference

Class reference

Here you can find a detailed documentation of all the settings and features of this class.

Variable Summary
string $delimiter
string $enclosure
string $filename
bool $header
string $item
array $map
string $output
string $url
string $xml

Method Summary
static void convert (string $url)
static void convertString$xmlString, string $url)
XmlToCsv __construct ([array $params = null])
mixed autoConvert ()
void mapConvert ([mixed $mapping = array()])
XmlToCsv setParams (array $params_array)
XmlToCsv __call (mixed $name, mixed $arguments)

string $delimiter = "," (line 95)

the character to use as CSV delimiter. Only 1 character.

  • access: public
string $enclosure = '"' (line 102)

the character to use to enclose items in the CSV . Only 1 character.

  • access: public
string $filename = "export.csv" (line 72)

the filename used for the newly created CSV, if $output is set to 'file'

  • access: public
bool $header = true (line 117)

set if the CSV header line should appear in the output

  • access: public
int $importFrom = 0 (line 80)

indicate FROM which entry to begin to export the data. This can be used if you want to export only some items from the XML, not all of them.
Defaults to 0.
If used together with $importTo, you can set an interval for selecting specific items from the XML

  • access: public
int $importTo = 99999999 (line 88)

indicate TO which entry to export the data. This can be used if you want to export only some items from the XML, not all of them.
Defaults to 999999999.
If used together with $importFrom, you can set an interval for selecting specific items from the XML

  • access: public
string $item = "/*/*" (line 169)

XPath expression to determine the Item nodes (=CSV rows) for the export Defaults to the 2nd child node in the XML

  • access: public
array $map = array() (line 161)

This key=>value paired array can be used to map XML nodes to CSV fields. By default all exported XML nodes are mapped to CSV fields with the same name.



This XML would mapped to the CSV like this:


If you wand to map the nodes to different fields, you have to pass a mapping to $map:

  $this->map = array(
                   'csv1' => 'node1',
                   'csv2' => 'node2',

This would then result in the follwing CSV export:

  • access: public
string $output = 'string' (line 111)

output type:

  • 'string' (default) - return as CSV string
  • 'file' - return as CSV file
  • 'echo' - write CSV out to the document
  • access: public
bool $selectedFields = false (line 124)

set if only the selected fields (defined in $map) should appear in the output. If FALSE - all fields will be put out

  • access: public
string $url = '' (line 58)

the URL to the XML file (local or remote). If $xml is filled, then $url is ignored

  • access: public
string $xml = '' (line 65)

the XML string, can be used to directly input the XML. If this is filled, the $url param is ignored

  • access: public
static method convert (line 355)

automaticaly convert XML from the specified URL and return as CSV string

  • access: public
static void convert (string $url)
  • string $url
static method convertString (line 366)

automaticaly convert XML from the specified XML string and return as CSV string

  • access: public
static void convertString $xmlString, string $url)
  • string $url
  • $xmlString
Constructor __construct (line 202)

The class constructor.

  • access: public
XmlToCsv __construct ([array $params = null])
  • array $params: - if this paramaters is a filled array, set up all paramters (equivalent to $this->setParams())
autoConvert (line 250)

This is the function which does the whole magic conversion.

Basically all that is needed is the XML source. With no other parameters, it automatically converts the XML to CSV. All previously set up paramtere are used to extend the default functionality.

  • return: - string - if $output is set to 'string' and the conversion is OK
    • bool - otherwise. True on success and false on failure
  • access: public
mixed autoConvert ()
mapConvert (line 346)

convert XML to CSV with a mapping setup - only the mapped fields will be exported.

If $mapping is empty, the previously set parameters and mapping is used. If no mapping was set, the function returns the result as $this->autoConvert()

void mapConvert ([mixed $mapping = array()])
  • mixed $mapping: - array of csvField=>xmlNode pairs
setParams (line 233)

method used to set more paramters at once

  • access: public
XmlToCsv setParams (array $params_array)
  • array $params_array: - array of paramName=>paramValue pairs
__call (line 186)
  • access: public
XmlToCsv __call (mixed $name, mixed $arguments)
  • mixed $name
  • mixed $arguments

Live demo

If you want to see the converter in action, check out the live demo: