Last active
September 12, 2018 05:10
-
-
Save mtvbrianking/4972aacb40e0cc8ab421d81901d49e46 to your computer and use it in GitHub Desktop.
Convert XML to Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class JsonXMLElement | |
* @link https://hakre.wordpress.com/2013/07/09/simplexml-and-json-encode-in-php-part-i/ | |
*/ | |
class JsonXMLElement extends SimpleXMLElement implements JsonSerializable | |
{ | |
/** | |
* Specify data which should be serialized to JSON | |
* | |
* @return mixed data which can be serialized by json_encode. | |
*/ | |
public function jsonSerialize() | |
{ | |
$array = array(); | |
// json encode attributes if any. | |
if ($attributes = $this->attributes()) { | |
$array['@attributes'] = iterator_to_array($attributes); | |
} | |
// json encode child elements if any. group on duplicate names as an array. | |
foreach ($this as $name => $element) { | |
if (isset($array[$name])) { | |
if (!is_array($array[$name])) { | |
$array[$name] = [$array[$name]]; | |
} | |
$array[$name][] = $element; | |
} else { | |
$array[$name] = $element; | |
} | |
} | |
// json encode non-whitespace element simplexml text values. | |
$text = trim($this); | |
if (strlen($text)) { | |
if ($array) { | |
$array['@text'] = $text; | |
} else { | |
$array = $text; | |
} | |
} | |
// return empty elements as NULL (self-closing or empty tags) | |
if (!$array) { | |
$array = NULL; | |
} | |
return $array; | |
} | |
} | |
$xml = <<<XML | |
<?xml version="1.0" encoding="UTF-8" ?> | |
<root doc="profile"> | |
<!-- This is a comment... --> | |
<first_name>Brian</first_name> | |
<middle_name></middle_name> | |
<last_name>Matovu</last_name> | |
<languages>Luganda</languages> | |
<languages>English</languages> | |
<website> | |
<![CDATA[http://bmatovu.com]]> | |
</website> | |
<frameworks> | |
<php>Laravel</php> | |
<php>Slim</php> | |
<python>django</python> | |
</frameworks> | |
<accounts> | |
<username>brian-admin</username> | |
<role>admin</role> | |
</accounts> | |
<accounts> | |
<username>bmatovu</username> | |
<role>supervisor</role> | |
</accounts> | |
<packages> | |
<multi-auth> | |
<language>php</language> | |
<frameworks>laravel</frameworks> | |
<home-page> | |
<![CDATA[https://github.com/mtvbrianking/multi-auth]]> | |
</home-page> | |
</multi-auth> | |
<laravel-xml> | |
<language>php</language> | |
<framework>laravel</framework> | |
<home-page> | |
<![CDATA[https://github.com/mtvbrianking/laravel-xml]]> | |
</home-page> | |
</laravel-xml> | |
</packages> | |
</root> | |
XML; | |
$obj = simplexml_load_string($xml, 'JsonXMLElement'); | |
// print_r($obj); | |
// print json_encode($obj, true); | |
print json_encode([$obj->getName() => $obj], true); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment