class Xml2Csv

@author Axel Roszkopf, WEBAX, 2013

version 1.0, 15.3.2013 This class is an easy to use, highly customisable XML to 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. Features: - easy conversion with only 1 parameter through static function call - file or string as XML input - CSV delimiter and enclosure character setting - custom mapping of XML nodes to CSV fields - custom export field selection - setting parameters easily by chained function calls or by accessing them directly - possibilty to mass-setup of paramters - set an interval to limit the number of processed items (from, to) Examples: <code> // Example 1 - easy automatic conversion XmlToCsv::convert('example.xml'); // Example 2 - chained parameter setting $x = new XmlToCsv(); echo $x->url('example.xml') ->output(false) ->autoConvert(); // Example 3 - parameters set separately by directly accessing the class variables $x = new XmlToCsv(); $x->url = 'example.xml'; $x->output = false; echo $x->autoConvert(); // Example 4 - parameters set at object creation as an array $x = new XmlToCsv(array( 'url'=>'example.xml', 'output'=>'echo', 'importTo'=>3, )); $x->autoConvert(); </code>

 Methods

__call()

__call(mixed $name, mixed $arguments) : \XmlToCsv

method

Parameters

$name

mixed

$arguments

mixed

Returns

The class constructor.

__construct(array $params) : \XmlToCsv

Parameters

$params

array
  • if this paramaters is a filled array, set up all paramters (equivalent to $this->setParams())

Returns

This is the function which does the whole magic conversion.

autoConvert() : mixed

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.

Returns

mixed- string - if $output is set to 'string' and the conversion is OK - bool - otherwise. True on success and false on failure

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

convert(string $url) 

Parameters

$url

string

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

convertString($xmlString) 

Parameters

$xmlString

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

mapConvert(mixed $mapping) 

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()

see \global\$map

Parameters

$mapping

mixed
  • array of csvField=>xmlNode pairs

method used to set more paramters at once

setParams(array $params_array) : \XmlToCsv

Parameters

$params_array

array
  • array of paramName=>paramValue pairs

Returns

internal method used to set a parameter value

_setParam(string $name, mixed $value) : \XmlToCsv

Parameters

$name

string

$value

mixed

Returns

 Properties

 

the character to use as CSV delimiter.

$delimiter : string

Only 1 character.

 

the character to use to enclose items in the CSV .

$enclosure : string

Only 1 character.

 

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

$filename : string

 

set if the CSV header line should appear in the output

$header : bool

 

indicate FROM which entry to begin to export the data.

$importFrom : int

This can be used if you want to export only some items from the XML, not all of them. Defaults to. 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.

$importTo : int

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

$item : string

 

This key=>value paired array can be used to map XML nodes to CSV fields.

$map : array

By default all exported XML nodes are mapped to CSV fields with the same name. Example:

  
      
          Text1
          Text2
      
  

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: - 'string' (default) - return as CSV string - 'file' - return as CSV file - 'echo' - write CSV out to the document - 'array' - return as an array prepared for further use

$output : string

 

set if only the selected fields (defined in $map) should appear in the output.

$selectedFields : bool

If FALSE - all fields will be put out

 

the URL to the XML file (local or remote).

$url : string

If $xml is filled, then $url is ignored

 

the XML string, can be used to directly input the XML.

$xml : string

If this is filled, the $url param is ignored