2017 © Pedro Peláez
 

library array2xml

Array -> XML Converter Class

image

dbrisinajumi/array2xml

Array -> XML Converter Class

  • Tuesday, January 30, 2018
  • by uldisn
  • Repository
  • 4 Watchers
  • 0 Stars
  • 746 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 18 Forks
  • 0 Open issues
  • 2 Versions
  • 2 % Grown

The README.md

[PHP] Array to XML

Array2xml is a PHP library that converts array to valid XML., (*1)

Based on XMLWriter., (*2)

Requirements

  • PHP 5.3+
  • XMLWriter

Installation

require_once ('/path/to/array2xml.php');

Usage (Ex.: RSS Last News)

Load the library and set custom configuration:, (*3)

$array2xml = new Array2xml();
$array2xml->setRootName('rss');
$array2xml->setRootAttrs(array('version' => '2.0'));
$array2xml->setCDataKeys(array('description'));

Start by creating a root element:, (*4)

$data['channel']['title']        = 'News RSS';
$data['channel']['link']         = 'http://yoursite.com/';
$data['channel']['description']  = 'Amazing RSS News';
$data['channel']['language']     = 'en';

Now pass elements from DB query in cycle:, (*5)

$row = $db->lastNews();
foreach($row as $key => $lastNews)
{
    $data['channel'][$key]['item']['title']       = $lastNews->title;
    $data['channel'][$key]['item']['link']        = 'http://yoursite.com/news/'.$lastNews->url;
    $data['channel'][$key]['item']['description'] = $lastNews->description;
    $data['channel'][$key]['item']['pubDate']     = date(DATE_RFC1123, strtotime($lastNews->added));
}

You can also set element attributes individually. The example below appends an attribute AttributeName to item node:, (*6)

$data['channel'][$key]['item']['@attributes']       = array('AttributeName' => $attributeValue);

Or, if your node doesn't have children, you can use this:, (*7)

$data['channel]['@attributes'] = array('AttributeName' => $attributeValue);
$data['channel]['@content']    = 'Content of channel node';

This will set both attributes and the content of channel node., (*8)

Alternatively, you can use setElementsAttrs() method:, (*9)

$array2xml->setElementsAttrs( array('ElementName' => array('AttributeName' => $attributeValue) ));

Note that in this case all elements with specified names will have identical attribute names and values., (*10)

If you need to include a raw XML tree somewhere, mark it's element using $array2xml->setRawKeys(array('elementName')), (*11)

Finally, convert and print output data to screen, (*12)

echo $array2xml->convert($data);

Configuration

You can easily configure this lib to fit your specific use case using setters described below., (*13)

setVersion(string $version)

Sets XML version header., (*14)

setEncoding(string $encoding)

Sets XML encoding, (*15)

setRootName(string $rootName)

Set XML Root Element Name, (*16)

setRootAttrs(array $rootAttrs)

Set XML Root Element Attributes, (*17)

setElementsAttrs(array $attrs)

Set Attributes of every XML Elements that matches the given names. Example argument: ['elementName' => ['someAttr' => 'attrValue']], (*18)

setCDataKeys(array $elementNames)

Marking given elements as CData ones, (*19)

setRawKeys(array $elementNames)

Marking given elements as raw ones, (*20)

setNumericTagPrefix(string $prefix)

Set default prefix for numeric nodes, (*21)

setSkipNumeric(bool $skipNumeric)

On/Off Skip numeric nodes, (*22)

setEmptyElementSyntax(const)

In some cases you might want to control the exact syntax of empty elements., (*23)

By default, nodes that are empty or equal to null are using self-closing syntax(<foo/>)., (*24)

You can override this behavior using Array2xml::EMPTY_FULL to force using closing tag(<foo></foo>)., (*25)

Available agruments are Array2xml::EMPTY_SELF_CLOSING or Array2xml::EMPTY_FULL, (*26)

setFilterNumbersInTags(bool|array $data)

Remove numbers from element names., (*27)

Possible args are:, (*28)

  • boolean TRUE to remove numbers from ALL elements
  • array contains node names that need filtering.

This is a easy workaround to have identically named elements in your XML built from an array., (*29)

For example, let's build an XML with 3 image nodes:, (*30)

//list of our images
$images = array('image1.jpg', 'image3.jpg', 'image3.jpg');
// input array
$data = array(); 
for($i=0;$i<3;$i++){
    $data['image'.$i] = $images[$i];
}
$array2xml = new array2xml();
$array2xml->setFilterNumbersInTags(array('image'));

$xml = array2xml->convert($data);

That's it! Now we have a nasty XML with 3 identically named nodes in it., (*31)

Testing

phpunit Tests/Array2xmlTest.php

The Versions

30/01 2018

dev-master

9999999-dev

Array -> XML Converter Class

  Sources   Download

BSD-2-Clause

by Anton Vasylyev

php xml

30/01 2018

0.9

0.9.0.0

Array -> XML Converter Class

  Sources   Download

BSD-2-Clause

by Anton Vasylyev

php xml