Skip to content

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

License

Notifications You must be signed in to change notification settings

muriloacs/EasyXML

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EasyXML

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

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.

Installation

With composer

  1. Add this project in your composer.json:

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

    $ php composer.phar update

Post installation

  1. Enabling it in your application.config.php file.

    <?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.

ControllerPlugin

--- XML TO ARRAY ---

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

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

$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 ---

$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);

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages