desperado/xml-bundle

Symfony XmlBundle

Installs: 141 821

Dependents: 2

Suggesters: 1

Security: 1

Stars: 9

Watchers: 3

Forks: 11

Open Issues: 4

Type:symfony-bundle

0.1.7 2015-04-16 10:04 UTC

This package is not auto-updated.

Last update: 2024-04-13 13:46:47 UTC


README

Build Status SensioLabsInsight Latest Stable Version License Total Downloads

XML Builder/Reader Bundle for Symfony2

Installation

Using composer

Add desperado/xml-bundle to your composer.json file.

     "require": {
	     "desperado/xml-bundle": "dev-master"
     }

The next thing you should do is install the bundle by executing the following command:

php composer.phar update desperado/xml-bundle

Finally, add the bundle to the registerBundles function of the AppKernel class in the app/AppKernel.php file:

public function registerBundles()
{
    $bundles = array(
        ...
        new Desperado\XmlBundle\DesperadoXmlBundle,
        ...
    );

Usage

DIC

  • XmlEditor: desperado_xml.model.xml_editor
  • XmlGenerator: desperado_xml.model.xml_generator
  • XmlReader: desperado_xml.model.xml_reader
  • XmlPrepare: desperado_xml.model.xml_prepare

Create xml from array

<?php

use Desperado\XmlBundle\Model\XmlGenerator;

$params = [
            'Request' => [
                '@ns'               => [
                    '0_xmlns' => ['prefix' => 'xsi', 'uri' => 'http//www.w3.org/2001/XMLSchema-instance'],
                    '1_xmlns' => ['prefix' => 'xsd', 'uri' => 'http//www.w3.org/2001/XMLSchema'],
                ],
                '@attrib'           => [
                    'Id'      => 100,
                    'Service' => 200,
                    'xmlns'   => 'http://ekassir.com/ekassir/PaySystem/Server/eKassirV3Protocol'
                ],
                'PaymentParameters' => [
                    '@attrib'   => ['xmlns' => ''],
                    'Parameter' => [
                        '@attrib' => ['Name' => 'account'],
                        '@value'  => 'emptyAccount'
                    ]

                ]
            ]
        ];


        $xmlGenerator = new XmlGenerator;

        echo $xmlGenerator->generateFromArray($params);

prints:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http//www.w3.org/2001/XMLSchema">
  <Request xmlns="http://ekassir.com/ekassir/PaySystem/Server/eKassirV3Protocol" xmlns:xsi="http//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http//www.w3.org/2001/XMLSchema" Id="100" Service="200" xsi:xmlns="" xsd:xmlns="">
    <PaymentParameters xmlns="">
      <Parameter Name="account">emptyAccount</Parameter>
    </PaymentParameters>
  </Request>
</root>

Create XML without attributes, namespaces, etc.

<?php

use Desperado\XmlBundle\Model\XmlPrepare;
use Desperado\XmlBundle\Model\XmlGenerator;

        $params = [
            'Details' => [
                'PaymentParameters' => [
                    'first_node'  => 'first_node_value',
                    'second_node' => 'second_node_value'
                ]
            ]
        ];


        $xmlPrepare = new XmlPrepare;
        $xmlGenerator = new XmlGenerator;

        echo $xmlGenerator->setRootName('request')->generateFromArray($xmlPrepare->prepareArrayBeforeToXmlConvert($params));

prints:

<?xml version="1.0" encoding="UTF-8"?>
<request>
  <Details>
    <PaymentParameters>
      <first_node>first_node_value</first_node>
      <second_node>second_node_value</second_node>
    </PaymentParameters>
  </Details>
</request>

Parse XML without attributes, namespaces, etc.

<?php

use Desperado\XmlBundle\Model\XmlReader;

        $xmlString = '<?xml version="1.0" encoding="UTF-8"?>
                      <request>
                          <Details>
                              <PaymentParameters>
                                  <first_node>first_node_value</first_node>
                                  <second_node>second_node_value</second_node>
                              </PaymentParameters>
                          </Details>
                      </request>';

        $xmlReader = new XmlReader;

        print_r($xmlReader->processConvert($xmlString));

prints:

Array
(
    [Details] => Array
        (
            [PaymentParameters] => Array
                (
                    [first_node] => first_node_value
                    [second_node] => second_node_value
                )

        )

)