PHP XML to JSON Conversion: Convert a XML document into a JSON string

Recommend this page to a friend!
  Info   View files Example   View files View files (29)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2020-08-12 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 92 This week: 8All time: 9,489 This week: 41Up
Version License PHP version Categories
xml-to-json 1.0.0Custom (specified...5XML, PHP 5, Files and Folders, Data t...
Description Author

This package can convert a XML document into a JSON string.

It can take string or a file with a XML document and parses it to extract its structure.

The package returns a string that represents the same document in the JSON format.

Innovation Award
PHP Programming Innovation award nominee
August 2020
Number 3
XML used to be a popular format to represent structured data that could be stored in files. Nowadays, JSON is a more popular format for the same purpose.

This package provides a solution that can be convert existing XML documents into equivalent documents that represent the same data structures in JSON format.

Manuel Lemos
Picture of Temuri Takalandze
  Performance   Level  
Name: Temuri Takalandze <contact>
Classes: 6 packages by
Country: Georgia Georgia
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

Details

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.

  Files folder image Files  
File Role Description
Files folder image.github (1 file)
Files folder imageexamples (3 files)
Files folder imagesrc (3 files)
Files folder imagetests (5 files, 1 directory)
Accessible without login Plain text file .coveralls.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  .github  
File Role Description
  Accessible without login Plain text file FUNDING.yml Data Auxiliary data

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file convert_file.php Example Example script
  Accessible without login Plain text file convert_string.php Example Example script
  Accessible without login Plain text file example.xml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
  Plain text file AbstractConverter.php Class Class source
  Plain text file FileConverter.php Class Class source
  Plain text file StringConverter.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imagemeta (2 directories)
  Plain text file AbstractConverterTest.php Class Class source
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file FileConverterTest.php Class Class source
  Plain text file StringConverterTest.php Class Class source
  Plain text file TestCase.php Class Class source

  Files folder image Files  /  tests  /  meta  
File Role Description
Files folder imagejson (5 files)
Files folder imagexml (5 files)

  Files folder image Files  /  tests  /  meta  /  json  
File Role Description
  Accessible without login Plain text file 1.json Data Auxiliary data
  Accessible without login Plain text file 2.json Data Auxiliary data
  Accessible without login Plain text file 3.json Data Auxiliary data
  Accessible without login Plain text file 4.json Data Auxiliary data
  Accessible without login Plain text file 5.json Data Auxiliary data

  Files folder image Files  /  tests  /  meta  /  xml  
File Role Description
  Accessible without login Plain text file 1.xml Data Auxiliary data
  Accessible without login Plain text file 2.xml Data Auxiliary data
  Accessible without login Plain text file 3.xml Data Auxiliary data
  Accessible without login Plain text file 4.xml Data Auxiliary data
  Accessible without login Plain text file 5.xml Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:92
This week:8
All time:9,489
This week:41Up

For more information send a message to info at phpclasses dot org.