2017 © Pedro Peláez
 

library easyxml

Zend Framework 2 module for dealing with XML. Creates XML from an array and vice versa.

image

muriloamaral/easyxml

Zend Framework 2 module for dealing with XML. Creates XML from an array and vice versa.

  • Thursday, March 26, 2015
  • by muriloamaral
  • Repository
  • 1 Watchers
  • 1 Stars
  • 99 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

EasyXML

Zend Framework 2 module for dealing with XML. Creates XML from an array and vice versa., (*1)

This module provides both a Service and a ControllerPlugin with which you can use to convert an array to XML and also a XML to an array., (*2)

Installation

With composer

  1. Add this project in your composer.json:, (*3)

    "require": {
        "muriloamaral/easyxml": "dev-master"
    }
    
  2. Now tell composer to download EasyXML by running the command:, (*4)

    $ php composer.phar update
    

Post installation

  1. Enabling it in your application.config.php file., (*5)

    <?php
    return array(
        'modules' => array(
            // ...
            'EasyXML',
        ),
        // ...
    );
    

Usage

You can either create an instance of EasyXMLthrough the ServiceManager or use it within your controller by using the EasyXML ControllerPlugin., (*6)

ControllerPlugin

--- XML TO ARRAY ---, (*7)

public function xml2arrayAction()
{
    // IT COULD BE EITHER A XML PATH FILE OR A STRING CONTAINING THE XML CONTENT.
    $xml = ROOT_PATH . '/data/my-xml-file.xml';

    $array = $this->easyXML()->xml2array($xml); 
}

--- ARRAY TO XML ---, (*8)

public function array2xmlAction()
{
    $rootNode = 'books';

    $body = array(
        '@attributes' => array(
            'type' => 'fiction'
        ),
        'book' => array(
            array(
                '@attributes' => array(
                    'author' => 'George Orwell'
                ),
                'title' => '1984'
            ),
            array(
                '@attributes' => array(
                    'author' => 'Isaac Asimov'
                ),
                'title' => array('@cdata'=>'Foundation'),
                'price' => '$15.61'
            ),
            array(
                '@attributes' => array(
                    'author' => 'Robert A Heinlein'
                ),
                'title' =>  array('@cdata'=>'Stranger in a Strange Land'),
                'price' => array(
                    '@attributes' => array(
                        'discount' => '10%'
                    ),
                    '@value' => '$18.00'
                )
            )
        )
    );

    $xml = $this->easyXML()->array2xml($rootNode, $body);
}

ServiceManager

--- XML TO ARRAY ---, (*9)

$xmlService = $this->getServiceLocator()->get('EasyXML');

// IT COULD BE EITHER A XML PATH FILE OR A STRING CONTAINING THE XML CONTENT.
$xml = ROOT_PATH . '/data/my-xml-file.xml'; 

$array = $xmlService->xml2array($xml);

--- ARRAY TO XML ---, (*10)

$xmlService = $this->getServiceLocator()->get('EasyXML');

$rootNode = 'books';

$body = array(
    '@attributes' => array(
        'type' => 'fiction'
    ),
    'book' => array(
        array(
            '@attributes' => array(
                'author' => 'George Orwell'
            ),
            'title' => '1984'
        ),
        array(
            '@attributes' => array(
                'author' => 'Isaac Asimov'
            ),
            'title' => array('@cdata'=>'Foundation'),
            'price' => '$15.61'
        ),
        array(
            '@attributes' => array(
                'author' => 'Robert A Heinlein'
            ),
            'title' =>  array('@cdata'=>'Stranger in a Strange Land'),
            'price' => array(
                '@attributes' => array(
                    'discount' => '10%'
                ),
                '@value' => '$18.00'
            )
        )
    )
);

$xml = $xmlService->array2xml($rootNode, $body);

The Versions

26/03 2015

dev-master

9999999-dev https://github.com/muriloacs/EasyXML

Zend Framework 2 module for dealing with XML. Creates XML from an array and vice versa.

  Sources   Download

The Requires

 

The Development Requires

xml zf2 array module zendframework