Recommend this page to a friend! |
Download .zip |
Info | Example | View files (29) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2020-08-12 (1 month ago) | Not yet rated by the users | Total: 92 This week: 8 | All time: 9,489 This week: 41 |
Version | License | PHP version | Categories | |||
xml-to-json 1.0.0 | Custom (specified... | 5 | XML, PHP 5, Files and Folders, Data t... |
Simple way to convert XML to JSON.
You can install this library with Composer:
Include composer autoloader in your main file (Ex.: index.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.
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"
// }
// }
// }
//}
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.
Please see CHANGELOG for details.
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.
Copyright © 2020 Temuri Takalandze. Released under the MIT license.
Files |
File | Role | Description | ||
---|---|---|---|---|
.github (1 file) | ||||
examples (3 files) | ||||
src (3 files) | ||||
tests (5 files, 1 directory) | ||||
.coveralls.yml | Data | Auxiliary data | ||
.travis.yml | Data | Auxiliary data | ||
CHANGELOG.md | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpunit.xml.dist | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files | / | examples |
File | Role | Description |
---|---|---|
convert_file.php | Example | Example script |
convert_string.php | Example | Example script |
example.xml | Data | Auxiliary data |
Files | / | src |
File | Role | Description |
---|---|---|
AbstractConverter.php | Class | Class source |
FileConverter.php | Class | Class source |
StringConverter.php | Class | Class source |
Files | / | tests |
File | Role | Description | ||
---|---|---|---|---|
meta (2 directories) | ||||
AbstractConverterTest.php | Class | Class source | ||
bootstrap.php | Aux. | Auxiliary script | ||
FileConverterTest.php | Class | Class source | ||
StringConverterTest.php | Class | Class source | ||
TestCase.php | Class | Class source |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.