Q:

Need Logic Assistance - JSON object

I parse the following XML as JSON:
XML
<categories>
    <cat id="0" caption="Category 0" img="">
    <cat id="1" caption="Category 1" img="">
        <cat pid="1" cid="0" caption="Sub Category 0" img="" />
        <cat pid="1" cid="1" caption="Sub Category 1" img="" />
    </cat>
    <cat id="2" caption="Category 2" img="">
        <cat pid="2" cid="0" caption="Sub Category 0" img="" />
        <cat pid="2" cid="1" caption="Sub Category 1" img="" />
    </cat>
</categories>

I got the following with jquery and it works but only for single cat's without sub cats.
JQuery
$.get("prop-cat.xml", function(xml) {
    var data = $.xml2json(xml);
    $.each(data, function() {
        $.each(this, function(i, value) {
            var outer = $('<div/>', {id: 'cat-' + value.cid, class: 'prop_cat'});
            var image = $('<div/>', {class: 'cat-image', src: value.img});
            var caption = $('<div/>', {class: 'cat-caption', text: value.caption});
            $(image).insertAfter($(caption));
            $(outer).append($(image));

            $(pb).append($(outer)); 
        });
    });
});

Anyone here who's got a clean solution, I am not sure about how I should write the logic for this.
I want it to be able to parse any and all categories wherever they are.
Like if I want to have a category with 20 child categories then it should parse them and display them in html, but it should also parse childs of those categories.
Pff!
Thanks

A:

This has been fixed with the following:
function ParseCategories(i, value) {
    if (value.pid != undefined) {
        console.log('Child category ' + value.cid + ' parsed, parent: ' + value.pid + '.');
    } else {
        console.log('Category ' + value.id + ' parsed.');
    }

    //parse childs and run function again
    if (value.cat != undefined) {
        $.each(value.cat, function(i, child) {
            ParseCategories(i, child);
        });
    }
}

function loadProps() {
    //variables
    var pb = $('div#prop_browser');
    //parse prop categories
    console.log('Parsing Categories....');
    $.get("prop-cat.xml", function(xml) {
        var data = $.xml2json(xml);
        $.each(data, function() {
            $.each(this, function(i, value) {
                //parse category
                ParseCategories(i, value);
            });
        });
    });

}

