3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class ObjectAndXML { private static $xml; // Constructor public function __construct() { $this->xml = new XmlWriter(); $this->xml->openMemory(); $this->xml->startDocument('1.0'); $this->xml->setIndent(true); } // Method to convert Object into XML string public function objToXML($obj) { $this->getObject2XML($this->xml, $obj); $this->xml->endElement(); return $this->xml->outputMemory(true); } // Method to convert XML string into Object public function xmlToObj($xmlString) { return simplexml_load_string($xmlString); } private function getObject2XML(XMLWriter $xml, $data) { foreach($data as $key => $value) { if(is_object($value)) { $xml->startElement($key); $this->getObject2XML($xml, $value); $xml->endElement(); continue; } else if(is_array($value)) { $this->getArray2XML($xml, $key, $value); } if (is_string($value)) { $xml->writeElement($key, $value); } } } private function getArray2XML(XMLWriter $xml, $keyParent, $data) { foreach($data as $key => $value) { if (is_string($value)) { $xml->writeElement($keyParent, $value); continue; } if (is_numeric($key)) { $xml->startElement($keyParent); } if(is_object($value)) { $this->getObject2XML($xml, $value); } else if(is_array($value)) { $this->getArray2XML($xml, $key, $value); continue; } if (is_numeric($key)) { $xml->endElement(); } } } } $obj = new ObjectAndXML(); $str = <<<STR <?xml version="1.0" encoding="utf-8"?> <records> <person> <name>XYZ</name> <age>28</age> <gender>Male</gender> </person> <person> <name>ABC</name> <age>25</age> <gender>Male</gender> </person> </records> STR; $recordsObj = $obj->xmlToObj($str); echo '<pre>'; var_dump($recordsObj);
Output for 7.0.0 - 7.2.0
Notice: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 8 Notice: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 9 Notice: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 10 Notice: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 11 <pre>object(SimpleXMLElement)#3 (1) { ["person"]=> array(2) { [0]=> object(SimpleXMLElement)#4 (3) { ["name"]=> string(3) "XYZ" ["age"]=> string(2) "28" ["gender"]=> string(4) "Male" } [1]=> object(SimpleXMLElement)#5 (3) { ["name"]=> string(3) "ABC" ["age"]=> string(2) "25" ["gender"]=> string(4) "Male" } } }
Output for 5.3.19 - 5.6.28
Strict Standards: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 8 Strict Standards: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 9 Strict Standards: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 10 Strict Standards: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 11 <pre>object(SimpleXMLElement)#3 (1) { ["person"]=> array(2) { [0]=> object(SimpleXMLElement)#4 (3) { ["name"]=> string(3) "XYZ" ["age"]=> string(2) "28" ["gender"]=> string(4) "Male" } [1]=> object(SimpleXMLElement)#5 (3) { ["name"]=> string(3) "ABC" ["age"]=> string(2) "25" ["gender"]=> string(4) "Male" } } }