Skip to content

Commit

Permalink
📝 Update README.
Browse files Browse the repository at this point in the history
  • Loading branch information
ABGEO committed May 11, 2020
1 parent 4f3971d commit 1be47f5
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,104 @@ 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
<?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:

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

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

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

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

```php
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:


```php
$converter = new FileConverter();
```

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

```php
$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`
```json
{
"profile": {
"firstName": "Temuri",
"lastName": "Takalandze",
"active": "true",
"position": {
"title": "Developer",
"department": {
"title": "IT"
}
}
}
}
```

**See full example [here](examples).**

## Changelog
Expand Down

0 comments on commit 1be47f5

Please sign in to comment.