承接 abgeo/xml-to-json 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

abgeo/xml-to-json

最新稳定版本:v1.0.0

Composer 安装命令:

composer require abgeo/xml-to-json

包简介

Simple way to convert XML to JSON

README 文档

README

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.

统计信息

  • 总下载量: 4.42k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-05-11