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
Inherited

method
inherited_from \XmlToCsv::__call()

Parameters

$name

mixed

$arguments

mixed

Returns

The class constructor.

__construct(array $params) : \XmlToCsv
Inherited

inherited_from \XmlToCsv::__construct()

Parameters

$params

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

Returns

get the value of a channel sub-element

__get(string $name) : mixed

Parameters

$name

string

name of the element

Returns

mixedvalue of the element

set some sub-element in the channel element

__set(string $name, mixed $value) 

Parameters

$name

string
  • name of the element

$value

mixed
  • value of the element (when this is the image element, then value must be an array)

This is the function which does the whole magic conversion.

autoConvert($map) : mixed

All previously set up paramtere are used to extend the default functionality.

Parameters

$map

Returns

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

this function is not used in this class

convert() 

this function is not used in this class

convertString() 

create a datetime string in RFC-822 format needed for RSS

formatDate(mixed $dateTimeString) : string

Parameters

$dateTimeString

mixed

Returns

stringdatetime in RFC-822 format

This method sets the image for the feed.

image(mixed $url_or_array, mixed $title, mixed $link) : \XmlToRss

It can be used in 2 different ways: 1. with 3 arguments: $this->image($url,$link,$title) - which sets the required elements of the image tag at once 2. with 1 array argument: $this->image(array $elements), where $elements is a key=>value pair of sub-elements of the image tag with its values

    // example 1
    $x = new XmlToRss();
    $x->image('http://example.com/image.png','http://example.com/image.png','Some image title')

    // example 2
    $x = new XmlToRss();
    $x->image(array(
          'url'    => 'http://example.com/image.png',
          'link'   => 'http://example.com/image.png',
          'title'  => 'Some image title',
          'height' => '88'
     ));

Parameters

$url_or_array

mixed

$title

mixed

$link

mixed

Returns

this function is not used in this class

mapConvert() 

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

setParams(array $params_array) : \XmlToCsv
Inherited

inherited_from \XmlToCsv::setParams()

Parameters

$params_array

array
  • array of paramName=>paramValue pairs

Returns

internal method used to set a parameter value

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

Parameters

$name

string

$value

mixed

Returns

 Properties

 

the property holds an array of key=>value pairs which will be rendered as sub-element into the channel element

$channel : array

 

the character to use as CSV delimiter.

$delimiter : string
Inherited

Only 1 character.

inherited_from \XmlToCsv::$$delimiter
 

the character to use to enclose items in the CSV .

$enclosure : string
Inherited

Only 1 character.

inherited_from \XmlToCsv::$$enclosure
 

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

$filename : string

 

set if the CSV header line should appear in the output

$header : bool
Inherited

inherited_from \XmlToCsv::$$header
 

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 (=RSS items) 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 RSS fields.

$map : array

By default none of the exported XML nodes are mapped to RSS fields. You have to specify exactly which XML field will be used as which RSS field The whole mapping is used to populate the ITEM elements of the RSS output. The header(=CHANNEL) element is set separatly, and is not mapped from the XML (maybe a todo for a next version)

Example:

  
      
          Text1
          Text2
      
  

You need to map the XML nodes to different fields, e.g. by passing a mapping to $map:

  $this->map = array(
                  'title' => 'node1',
                  'description' => 'node2',
               )

The array (KEY => VALUE pair) defines which element in the RSS (=KEY) should be filled with which element from the XML (=VALUE). So in the example above - the TITLE element in the RSS will be filled with the value from NODE1 in the XML The example XML would be mapped to the RSS like this:

... Text1 Text2 ...

 

output type: - 'echo' (default) - write the RSS out to the document (with all the needed headers!) - 'string' - return the RSS as a string - 'file' - return as RSS XML file - 'array' - return as an array prepared for further use (only the items are returned)

$output : string

 

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

$selectedFields : bool
Inherited

If FALSE - all fields will be put out

inherited_from \XmlToCsv::$$selectedFields
 

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