, , JSON .
JSON, , . , XML.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<lss>
<image type="Profile Image" part_id="11017" media="Image1.jpg" base="Image1" ext="jpg" width="128" height="96"/>
<image type="Profile Image" part_id="11016" media="Image2.jpg" base="Image2" ext="jpg" width="180" height="225"/>
</lss>
:
{
"image":[
{
"type":"Profile Image",
"part_id":"11017",
"media":"Image1.jpg",
"base":"Image1",
"ext":"jpg",
"width":"128",
"height":"96"
},
{
"type":"Profile Image",
"part_id":"11016",
"media":"Image2.jpg",
"base":"Image2",
"ext":"jpg",
"width":"180",
"height":"225"
}
]
}
, $final_tree, "" .
function xml2json($xmlString)
{
$start_tree = (array) simplexml_load_string(trim($xmlString));
$final_tree = array();
loopRecursivelyForAttributes($start_tree,$final_tree);
return json_encode($final_tree);
}
function loopRecursivelyForAttributes($start_tree,&$final_tree)
{
foreach ($start_tree as $key1=>$row1)
{
if(!array_key_exists($key1, $final_tree))
{
$final_tree[$key1] = array();
}
if(array_key_exists('@attributes', $row1))
{
$row1 = (array) $row1;
getValues($start_tree,$final_tree, $key1, $row1);
}
else
{
foreach ($row1 as $row2)
{
$row2 = (array) $row2;
getValues($start_tree,$final_tree, $key1, $row2);
}
}
}
}
function getValues($start_tree,&$final_tree, $key1, $row2)
{
foreach ($row2 as $key3=>$val3)
{
$val3 = (array) $val3;
if($key3 == '@attributes')
{
$final_tree[$key1][] = $val3;
}
else
{
$temp_parent = array();
$temp_parent[$key3] = $val3;
loopRecursivelyForAttributes($temp_parent,$final_tree[$key1][count($final_tree[$key1])-1]);
}
}
}