Class XmlToCsv

Description

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
    • 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:

    1.     // Example 1 - easy automatic conversion
    2.     XmlToCsv::convert('example.xml');
    3.  
    4.     // Example 2 -  chained parameter setting
    5.     $x new XmlToCsv();
    6.     echo $x->url('example.xml')
    7.            ->output(false)
    8.            ->autoConvert();
    9.  
    10.     // Example 3 - parameters set separately by directly accessing the class variables
    11.     $x new XmlToCsv();
    12.     $x->url 'example.xml';
    13.     $x->output false;
    14.     echo $x->autoConvert();
    15.  
    16.     // Example 4 - parameters set at object creation as an array
    17.     $x new XmlToCsv(array(
    18.             'url'=>'example.xml',
    19.             'output'=>'echo',
    20.             'importTo'=>3,
    21.         ));
    22.     $x->autoConvert();

Located in /XmlToCsv.class.php (line 51)


	
			
Variable Summary
string $delimiter
string $enclosure
string $filename
bool $header
int $importTo
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)
Variables
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. 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.

Example:

  1.    <root>
  2.        <item>
  3.            <node1>Text1</node1>
  4.            <node2>Text2</node2>
  5.        </item>
  6.    </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:

  1.    $this->map array(
  2.                    'csv1' => 'node1',
  3.                    'csv2' => 'node2',
  4.                 )

This would then result in the follwing CSV export:

     csv1,csv2
     Text1,Text2

  • 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
Methods
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

Documentation generated on Fri, 15 Mar 2013 00:30:47 +0100 by phpDocumentor 1.4.2