Giter Club home page Giter Club logo

xml-to-json's Introduction

xml-to-json

Simple way to convert XML to JSON.

Build Status Coverage Status GitHub release Packagist Version GitHub license

Installation

You can install this library with Composer:

  • composer require abgeo/xml-to-json

Usage

Include composer autoloader in your main file (Ex.: index.php)

  • require __DIR__.'/../vendor/autoload.php';

This package gives you the ability to convert XML string/file to JSON string/file.
For this, we have two converters:

  • ABGEO\XmlToJson\StringConverter
  • ABGEO\XmlToJson\FileConverter

Let's look at them in action.

Convert XML String to JSON string

Create simple XML file:

example.xml

<?xml version="1.0" encoding="UTF-8"?>
<profile>
    <firstName>Temuri</firstName>
    <lastName>Takalandze</lastName>
    <active>true</active>
    <position>
        <title>Developer</title>
        <department>
            <title>IT</title>
        </department>
    </position>
</profile>

Create an object of class ABGEO\XmlToJson\StringConverter and read the content of example.xml into a variable:

$converter = new StringConverter();
$xmlContent = file_get_contents(__DIR__ . '/example.xml');

Now you can convert value of $xmlContent variable to JSON object:

$jsonContent = $converter->convert($xmlContent);

if you print this variable, you will get the following result:

echo $jsonContent;

//{
//    "profile": {
//    "firstName": "Temuri",
//        "lastName": "Takalandze",
//        "active": "true",
//        "position": {
//        "title": "Developer",
//            "department": {
//            "title": "IT"
//            }
//        }
//    }
//}

Convert XML file to JSON file

Consider that you already have the example.xml file described in the step above. Now let's create an object of ABGEO\XmlToJson\FileConverter class:

$converter = new FileConverter();

Using the convert method of this object, you can simply convert the XML file to a JSON file:

$converter->convert(__DIR__ . '/example.xml', __DIR__ . '/example.json');

Convert() takes two arguments - the path to the input and output files. If you do not specify an output file, by default it will be {$inputFile}.json.

Finally, the Convert () method will generate a new example.json with the following content:

example.json

{
    "profile": {
        "firstName": "Temuri",
        "lastName": "Takalandze",
        "active": "true",
        "position": {
            "title": "Developer",
            "department": {
                "title": "IT"
            }
        }
    }
}

See full example here.

Changelog

Please see CHANGELOG for details.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Authors

License

Copyright © 2020 Temuri Takalandze.
Released under the MIT license.

xml-to-json's People

Contributors

abgeo avatar dependabot-preview[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

xml-to-json's Issues

Can't pull into PHP 8 project

Any chance you could tag the composer project with PHP 8 compatibility? I've found this incredibly useful for a project, but it runs within a framework requiring PHP 8, and this one package is messing up my composer updates. (I have found no issues running it under PHP8.)

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.