An open API service indexing awesome lists of open source software.

https://github.com/krosscode/xml-to-object

PHP library for converting XML contents to objects
https://github.com/krosscode/xml-to-object

composer-package converter object php php-library php7 xml

Last synced: 2 months ago
JSON representation

PHP library for converting XML contents to objects

Awesome Lists containing this project

README

        

# XML to object

This library simply converts XML into an object.

## Getting started

XML to object is very easy to use. **Example:**

```php
// Using composer here, but you can also directly include XmlToObject.php
require_once __DIR__ . '/vendor/autoload.php';

// Import the function
use function KrossCode\XmlToObject\xmlToObject;

// First we need to get the contents of our XML file
$xmlContents = file_get_contents('path/to/xml/file.xml');
if (!$xmlContents) return false; // File couldn't be read

// Now we try to convert the XML to an object
$xmlObject = xmlToObject($xmlContents);
if (!$xmlObject) return false; // XML couldn't be converted to an object

print_r($xmlObject); // Success!
```

## Example

What does the object look like, compared to the XML?

### XML

```xml

Johnny's Green Goods


Cucumber
63
1.99


Carrot
24
1.79

```

### Object (represented in JSON)

```json
{
"Name": "Johnny's Green Goods",
"GroceryCollection": {
"Grocery": [
{
"@attributes": {
"id": "13"
},
"Name": "Cucumber",
"Amount": "63",
"Price": "1.99"
},
{
"@attributes": {
"id": "17"
},
"Name": "Carrot",
"Amount": "24",
"Price": "1.79"
}
]
}
}
```