2017 © Pedro Peláez
 

library xml-builder

A simple PHP based XML builder

image

aaronddm/xml-builder

A simple PHP based XML builder

  • Saturday, December 30, 2017
  • by AaronDDM
  • Repository
  • 1 Watchers
  • 0 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 63 % Grown

The README.md

PHP XML Builder Library

This is a simple PHP 7.1+ based XML Builder library. Use it to easily generate XML output with just PHP., (*1)

Build Status, (*2)

Table of Contents

  1. Installation
  2. Prerequisites
  3. Basic Usage
    1. Example: Using XMLWriter
    2. Output
  4. Looping through data
    1. Example: XML output of a list of users
    2. Output
  5. Using a custom "XMLElementData" class
    1. Example: Customized MyXMLElementData class
    2. Output
  6. Running tests
  7. License

Installation

composer require aaronddm/xml-builder

Prerequisites

  • PHP >=7.1.0
  • php-xml if using XMLWriter

Basic Usage

The following is an example of the most basic usage of this library., (*3)

Example: Using XMLWriter

createXMLArray()
            ->start('Root')
                ->addCData('1 First Child First Element', 'This is a test')
                ->add('First Child Second Element', false)
                ->start('Second Parent')
                    ->add('Second child 1', null, ['myAttr' => 'Attr Value'])
                    ->add('Second child 2', false)
                    ->start('Third Parent')
                        ->add('Child')
                    ->end()
                ->end()
                ->add('First Child Third Element')
            ->end();

    var_dump($xmlBuilder->getXML());
} catch (XMLArrayException $e) {
    var_dump('An exception occurred: ' . $e->getMessage());
}
```

### Output
```
string(414) "
<Root>
    <FirstChildFirstElement><![CDATA[This is a test]]></FirstChildFirstElement>
    <FirstChildSecondElement>False</FirstChildSecondElement>
    <SecondParent>
        <Secondchild myAttr="Attr Value"/>
        <Secondchild>False</Secondchild>
        <ThirdParent>
            <Child/>
        </ThirdParent>
    </SecondParent>
    <FirstChildThirdElement/>
</Root>
"

Looping through data

You easily added sets of data using the startLoop method provided., (*4)

Example: XML output of a list of users

 'John Doe',
        'age' => 32
    ],
    [
        'name' => 'Jane Doe',
        'age' => 98
    ]
];


$xmlWriterService = new XMLWriterService();
$xmlBuilder = new XMLBuilder($xmlWriterService);

$xmlBuilder
    ->createXMLArray()
        ->start('Root')
            ->startLoop('Users', [], function (XMLArray $XMLArray) use ($users) {
                foreach ($users as $user) {
                    $XMLArray->start('User')
                        ->add('name', $user['name'])
                        ->add('age', $user['age']);
                }
            })
            ->end()
        ->end();

var_dump($xmlBuilder->getXML());
```

### Output
```
string(261) "
<Root>
    <Users>
        <User>
            <name>John Doe</name>
            <age>32</age>
        </User>
        <User>
            <name>Jane Doe</name>
            <age>98</age>
        </User>
    </Users>
</Root>
"

Using a custom "XMLElementData" class

You can override the XMLElementData element class to implement transformations to the value of your data based on the type passed. To do this, you simply extend the XMLElementData class and override any of the methods to your liking., (*5)

Example: Customized MyXMLElementData class

getType();
        $value = $this->value;

        if(is_bool($value)) {
            $type = 'boolean';
        }

        switch($type) {
            case 'specialType':
                $value = 'Special Type Value';
                break;
            case 'boolean':
                $value = ($value) ? 'True' : 'False';
                break;
        }

        return $value;
    }
}

$xmlWriterService = new XMLWriterService();
$xmlBuilder = new XMLBuilder($xmlWriterService);
$xmlBuilder->setElementDataClass(MyXMLElementData::class);

try {
    $xmlBuilder
        ->createXMLArray()
            ->start('Root')
                ->addCData('1 First Child First Element', 'This is a test')
                ->add('First Child Second Element', false)
                ->start('Second Parent')
                    ->add('Second child 1', null, ['myAttr' => 'Attr Value'])
                    ->add('Second child 2', false)
                    ->start('Third Parent')
                        ->add('Child')
                        ->add('Special Type Child', "1", [], 'specialType')
                    ->end()
                ->end()
                ->add('First Child Third Element')
            ->end();

    var_dump($xmlBuilder->getXML());
} catch (XMLArrayException $e) {
    var_dump('An exception occurred: ' . $e->getMessage());
}
```

### Output
```
string(482) "
<Root>
    <FirstChildFirstElement><![CDATA[This is a test]]></FirstChildFirstElement>
    <FirstChildSecondElement>False</FirstChildSecondElement>
    <SecondParent>
        <Secondchild myAttr="Attr Value"/>
        <Secondchild>False</Secondchild>
        <ThirdParent>
            <Child/>
            <SpecialTypeChild>Special Type Value</SpecialTypeChild>
        </ThirdParent>
    </SecondParent>
    <FirstChildThirdElement/>
</Root>
"

Running tests

cd /root/of/project/
vendor/bin/phpunit

License

This project is open-sourced software licensed under the MIT license., (*6)

The Versions

30/12 2017

dev-master

9999999-dev

A simple PHP based XML builder

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

The Development Requires

by Aaron de Mello

30/12 2017

dev-develop

dev-develop

A simple PHP based XML builder

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

The Development Requires

by Aaron de Mello

30/12 2017

v1.0.1

1.0.1.0

A simple PHP based XML builder

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

The Development Requires

by Aaron de Mello

28/12 2017

v1.0.0

1.0.0.0

A simple PHP based XML builder

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

The Development Requires

by Aaron de Mello