Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Basically, you'll have to do as the other respondents suggest and format the JSON output yourself.</p> <p>This gives me the JSON I'm after - pretty similar to what you want. It's recursive, so it will work for any XML.</p> <p>This XML:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;lss&gt; &lt;image type="Profile Image" part_id="11017" media="Image1.jpg" base="Image1" ext="jpg" width="128" height="96"/&gt; &lt;image type="Profile Image" part_id="11016" media="Image2.jpg" base="Image2" ext="jpg" width="180" height="225"/&gt; &lt;/lss&gt; </code></pre> <p>Is formatted like this:</p> <pre><code> { "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" } ] } </code></pre> <p>That might be fine for you, but if not you'd only have to modify $final_tree slightly to get the array of naked "box" objects on the top level.</p> <pre><code> 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,&amp;$final_tree) { foreach ($start_tree as $key1=&gt;$row1) { if(!array_key_exists($key1, $final_tree)) { $final_tree[$key1] = array(); } // If there is only one sub node, then there will be one less // array - ie: $row1 will be an array which has an '@attributes' key 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,&amp;$final_tree, $key1, $row2) { foreach ($row2 as $key3=&gt;$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]); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload