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
- Host: GitHub
- URL: https://github.com/krosscode/xml-to-object
- Owner: krosscode
- License: gpl-3.0
- Created: 2019-11-15T09:49:34.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-11-15T11:48:23.000Z (over 5 years ago)
- Last Synced: 2024-12-25T09:18:29.514Z (4 months ago)
- Topics: composer-package, converter, object, php, php-library, php7, xml
- Language: PHP
- Size: 12.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 objectprint_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"
}
]
}
}
```