Xml2Csv is a helper class for easy XML to CSV conversion.
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
<? XmlToCsv::convert('example.xml'); ?>
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"?> <Shop> <Item> <Id>123456789</Id> <Desc>Some item description</Desc> <Link>http://some.link.com</Link> <Category>Category/Subcategory1</Category> </Item> <Item> <Id>96521354</Id> <Desc>Some other item description, which is much longer.</Desc> <Link>http://some-other.link.com</Link> <Category>Category/Subcategory2</Category> </Item> </Shop>"; XmlToCsv::convertString('example.xml');
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(); $x->url('example.xml') ->delimiter(';') ->output('file') ->autoConvert();
To start converting, just 2 simple steps are needed.
<?php require_once("Xml2Csv.class.php"); ?>
<? // if your file is on the server, just use the path to the file XmlToCsv::convert('example.xml'); // if you want to convert from a remote URL, use the whole URL XmlToCsv::convert('http://www.mysite.com/example.xml'); ?>
For more advanced examples refer to the 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') ->output(false) ->autoConvert();
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( 'url'=>'example.xml', 'output'=>'false', )); 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( 'url'=>'example.xml', )); $x->output = false; echo $x->delimiter(";") ->autoConvert();
For a list of all features and settings refer to the Class reference
the character to use as CSV delimiter. Only 1 character.
the character to use to enclose items in the CSV . Only 1 character.
the filename used for the newly created CSV, if $output is set to 'file'
set if the CSV header line should appear in the output
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
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
XPath expression to determine the Item nodes (=CSV rows) for the export Defaults to the 2nd child node in the XML
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.
Example:
<root> <item> <node1>Text1</node1> <node2>Text2</node2> </item> </root>
This XML would mapped to the CSV like this:
node1,node2 Text1,Text2
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:
csv1,csv2 Text1,Text2
output type:
set if only the selected fields (defined in $map) should appear in the output. If FALSE - all fields will be put out
the URL to the XML file (local or remote). If $xml is filled, then $url is ignored
the XML string, can be used to directly input the XML. If this is filled, the $url param is ignored
automaticaly convert XML from the specified URL and return as CSV string
automaticaly convert XML from the specified XML string and return as CSV string
The class constructor.
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.
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()
method used to set more paramters at once
Simple:
http://praca.webax.sk/xml2csv/simple.php
Advanced: