Code Monkey home page Code Monkey logo

xml-converter's Introduction

Xml Converter - convert from xml to array and viceversa

Latest Version on Packagist

A package that allows you to easily convert your XML string into array format or generate an XML string from an array. This package is based on: Spatie Array to XML and Gaarf XML string to PHP array.


Installation

You can install the package via composer:

composer require dmb/xml-converter

From Array to XML


Below some examples of use.

Default root name: root

$arrayConverted = (new FromArray())
    ->convertToXml($arrayToConvert);

Custom root name: customRootName

$arrayConverted = (new FromArray())
    ->convertToXml(
        $arrayToConvert,
        'customRootName'
    );

Custom root name: customRootName and attributes

$arrayConverted = (new FromArray())
    ->convertToXml(
        $arrayToConvert,
        [
        'rootElementName' => 'customRootName',
        '_attributes' => [
            'xmlns' => 'https://github.com/davidemariabusi/xml-converter',
        ],
    );

Example with all features

$arrayToConvert = [
    'First_User' => [
        '_attributes' => [
            'attr1' => 'value'
        ],
        'name' => 'Name',
    ],
    'Second_User' => [
        'name' => 'Name 2'
    ],
    'Third_User' => [
        '_attributes' => [
            'attr2' => 'value 2'
        ],
        '_value' => 'Name 3'
    ]
];

$arrayConverted = (new FromArray())
    ->convertToXml(
        $arrayToConvert,
        [
            'rootElementName' => 'customRootName',
            '_attributes' => [
                'xmlns' => 'https://github.com/davidemariabusi/xml-converter',
            ],
        ]
    );

Result

<?xml version="1.0"?>
<customRootName xmlns="https://github.com/davidemariabusi/xml-converter">
    <First_User attr1="value">
        <name>Name</name>
        <weapon>Lightsaber</weapon>
    </First User>
    <Second_User>
        <name>Name2</name>
    </Second_User>
    <Third_User attr2="value 2">
        Name 3
    </Third_User>
</customRootName>

From XML to Array


Below some examples of use.

With valid XML

The xml will be converted to array.

try {
    $converted = (new FromXml())
        ->convertToArray($validXml);
} catch (XmlParsingExcpetion $e) {
    $error = $e->getMessage();
}

With invalid XML

An XmlParsingExcpetion will be caught.

try {
    $converted = (new FromXml())
        ->convertToArray($invalidXML);
} catch (XmlParsingExcpetion $e) {
    $error = $e->getMessage();
}

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

xml-converter's People

Contributors

davidemariabusi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.