SimpleXMLTool - tool to generate a simple XML document, convert XML to Array and JSON
$data = array(
'val1' => 111,
'val2' => '222',
'val3' => 333,
'container' => array(
'mystr' => 'test test',
'myobj' => array(
'x' => 250,
'y' => 150,
'name' => 'objName',
'wtfs' => array(
'wtf' => array(
0 => array(
'x' => 111,
'y' => 111,
),
1 => array(
'x' => 222,
'y' => 222,
),
2 => array(
'x' => 333,
'y' => 333,
),
),
)
)
)
);
Not parse integer keys of array!
code:
echo $xml = SimpleXMLTool::createXML($data);
out:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<val1>111</val1>
<val2>222</val2>
<val3>333</val3>
<container>
<mystr>test test</mystr>
<myobj>
<x>250</x>
<y>150</y>
<name>objName</name>
<wtfs>
<wtf>
<x>111</x>
<y>111</y>
</wtf>
<wtf>
<x>222</x>
<y>222</y>
</wtf>
<wtf>
<x>333</x>
<y>333</y>
</wtf>
</wtfs>
</myobj>
</container>
</root>
code:
echo SimpleXMLTool::XMLtoJSON($xml);
out:
{
"val1":"111",
"val2":"222",
"val3":"333",
"container":{
"mystr":"test test",
"myobj":{
"x":"250",
"y":"150",
"name":"objName",
"wtfs":{
"wtf":[
{
"x":"111",
"y":"111"
},
{
"x":"222",
"y":"222"
},
{
"x":"333",
"y":"333"
}
]
}
}
}
}
code:
print_r(SimpleXMLTool::XMLtoArr($xml));
out:
Array
(
[val1] => 111
[val2] => 222
[val3] => 333
[container] => Array
(
[mystr] => test test
[myobj] => Array
(
[x] => 250
[y] => 150
[name] => objName
[wtfs] => Array
(
[wtf] => Array
(
[0] => Array
(
[x] => 111
[y] => 111
)
[1] => Array
(
[x] => 222
[y] => 222
)
[2] => Array
(
[x] => 333
[y] => 333
)
)
)
)
)
)