@ 2015-11-17T19:03:59Z <?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>
<attr>
<a id="2">2</a>
<a id="3">3</a>
</attr>
</records>
STR;
$recordsObj = $obj->xmlToObj($str);
echo '<pre>';
var_dump($recordsObj);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Notice: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 8
Deprecated: Creation of dynamic property ObjectAndXML::$xml is deprecated in /in/PZFb1 on line 8
Notice: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 9
Notice: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 10
Notice: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 11
<pre>object(SimpleXMLElement)#3 (2) {
["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"
}
}
["attr"]=>
object(SimpleXMLElement)#6 (1) {
["a"]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "3"
}
}
}
Output for 7.0.0 - 7.0.20 , 7.1.0 - 7.1.20 , 7.2.0 - 7.2.33 , 7.3.16 , 7.3.18 - 7.3.31 , 7.4.0 - 7.4.4 , 7.4.6 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 Notice: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 8
Notice: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 9
Notice: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 10
Notice: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 11
<pre>object(SimpleXMLElement)#3 (2) {
["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"
}
}
["attr"]=>
object(SimpleXMLElement)#6 (1) {
["a"]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "3"
}
}
}
Output for 7.3.17 , 7.4.5 Notice: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 8
Notice: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 9
Notice: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 10
Notice: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 11
<pre>object(SimpleXMLElement)#3 (2) {
["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"
}
}
["attr"]=>
object(SimpleXMLElement)#6 (1) {
["a"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#7 (2) {
["@attributes"]=>
array(1) {
["id"]=>
string(1) "2"
}
[0]=>
string(1) "2"
}
[1]=>
object(SimpleXMLElement)#8 (2) {
["@attributes"]=>
array(1) {
["id"]=>
string(1) "3"
}
[0]=>
string(1) "3"
}
}
}
}
Output for 7.3.32 - 7.3.33 <pre>object(SimpleXMLElement)#3 (2) {
["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"
}
}
["attr"]=>
object(SimpleXMLElement)#6 (1) {
["a"]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "3"
}
}
}
Output for 5.5.24 - 5.5.35 , 5.6.8 - 5.6.28 Strict Standards: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 8
Strict Standards: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 9
Strict Standards: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 10
Strict Standards: Accessing static property ObjectAndXML::$xml as non static in /in/PZFb1 on line 11
<pre>object(SimpleXMLElement)#3 (2) {
["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"
}
}
["attr"]=>
object(SimpleXMLElement)#6 (1) {
["a"]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "3"
}
}
}
preferences:dark mode live preview
124.93 ms | 404 KiB | 183 Q