__call()
__construct()
autoConvert()
convert()
convertString()
mapConvert()
setParams()
_setParam()
$delimiter
$enclosure
$filename
$header
$importFrom
$importTo
$item
$map
$output
$selectedFields
$url
$xml
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> |
---|
__construct(array $params) : \XmlToCsv
array
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.
mixed
- string - if $output is set to 'string' and the conversion is OK - bool - otherwise. True on success and false on failureconvert(string $url)
string
convertString($xmlString)
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 |
---|
mixed
$delimiter : string
Only 1 character.
$enclosure : string
Only 1 character.
$filename : string
$header : bool
$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
$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
$item : string
$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 : string
$selectedFields : bool
If FALSE - all fields will be put out
$url : string
If $xml is filled, then $url is ignored
$xml : string
If this is filled, the $url param is ignored