kenlog/converter

简单的XML到CSV、JSON或数组的转换工具

dev-master 2024-06-25 12:48 UTC

This package is auto-updated.

Last update: 2024-09-25 13:24:49 UTC


README

GitHub Packagist Downloads

Converter 📄 🔄 📃

简单的XML到CSV、JSON或数组的转换工具

入门指南

使用composer安装

默认情况下,composer将下载最新稳定版本。

composer require kenlog/converter

用法 - 示例

示例转换为CSV

<?php

require 'vendor/autoload.php';

use Kenlog\Converter;

// @constructor new Converter(string $xml);
$converterCSV = new Converter('products.xml');

// @method setCSV(string $csv, string $xpath, array $columns, string $separator = ',', int $skipLines = 0);
$converterCSV->setCSV('products.csv','//Product',['Code', 'Description', 'Um', 'Qty']);

// @var bool success
if ($converterCSV->success) {
    echo 'The XML file has been converted to CSV';
} else {
    echo 'The XML file was not converted to CSV';
}

示例转换为JSON

<?php

require 'vendor/autoload.php';

use Kenlog\Converter;

// @constructor new Converter(string $xml);
$converterJSON = new Converter('products.xml');

// @method setJSON(string $jsonFile);
$converterJSON->setJSON('products.json');

// @var bool success
if ($converterJSON->success) {
    echo 'The XML file has been converted to JSON';
} else {
    echo 'The XML file was not converted to JSON';
}

示例转换为数组

<?php

require 'vendor/autoload.php';

use Kenlog\Converter;

// @constructor new Converter(string $xml);
$converterArray = new Converter('products.xml');

// @array getArray();
$products = $converterArray->getArray();

// The result will be an array containing all records
foreach ($products['Product'] as $product) {
    echo $product['Code'] . PHP_EOL;
    echo $product['Description'] . PHP_EOL;
    echo $product['Um'] . PHP_EOL;
    echo $product['Qty'] . PHP_EOL;
}

👷 任何贡献都将受到高度赞赏

克隆仓库

git clone https://github.com/kenlog/converter.git

🐛 问题

创建一个问题,如果您发现了任何错误。

作者

许可证

本项目采用MIT许可证 - 有关详细信息,请参阅LICENSE.md文件。