Search code examples
javaxmlprestashopjtree

Java JTree / Prestashop categories


I am not familiar with JTree component and XML. I Have an XML File which represents Categories in Prestashop ecommerce platform :

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<categories>
    <category>
        <id><![CDATA[1]]></id>
        <id_parent><![CDATA[0]]></id_parent>
        <level_depth><![CDATA[0]]></level_depth>
        <nb_products_recursive notFilterable="true"><![CDATA[83]]></nb_products_recursive>
        <active><![CDATA[1]]></active>
        <id_shop_default><![CDATA[1]]></id_shop_default>
        <is_root_category><![CDATA[0]]></is_root_category>
        <position><![CDATA[1]]></position>
        <date_add><![CDATA[2015-09-08 09:40:14]]></date_add>
        <date_upd><![CDATA[2015-09-08 09:40:14]]></date_upd>
        <name>
        <language id="1" xlink:href="https://mywebsite/api/languages/1"><![CDATA[Racine]]></language>
        <language id="3" xlink:href="https://mywebsite/api/languages/3"><![CDATA[Racine]]></language>
        <language id="4" xlink:href="https://mywebsite/api/languages/4"><![CDATA[Racine]]></language>
        </name>
        <associations>
            <categories nodeType="category" api="categories">
                <category xlink:href="https://mywebsite/api/categories/2">
                    <id><![CDATA[2]]></id>
                </category>
            </categories>
            <products nodeType="product" api="products"/>
        </associations>
    </category>
    <category>
        <id><![CDATA[2]]></id>
        <id_parent xlink:href="https://mywebsite/api/categories/1"><![CDATA[1]]></id_parent>
        <level_depth><![CDATA[1]]></level_depth>
        <nb_products_recursive notFilterable="true"><![CDATA[83]]></nb_products_recursive>
        <active><![CDATA[1]]></active>
        <id_shop_default><![CDATA[1]]></id_shop_default>
        <is_root_category><![CDATA[1]]></is_root_category>
        <position><![CDATA[1]]></position>
        <date_add><![CDATA[2015-09-08 09:40:14]]></date_add>
        <date_upd><![CDATA[2015-09-08 09:40:14]]></date_upd>
        <name>
        <language id="1" xlink:href="https://mywebsite/api/languages/1"><![CDATA[Accueil]]></language>
        <language id="3" xlink:href="https://mywebsite/api/languages/3"><![CDATA[Accueil]]></language>
        <language id="4" xlink:href="https://mywebsite/api/languages/4"><![CDATA[Accueil]]></language>
        </name>
        <associations>
            <categories nodeType="category" api="categories">
                <category xlink:href="https://mywebsite/api/categories/15">
                    <id><![CDATA[15]]></id>
                </category>
                <category xlink:href="https://mywebsite/api/categories/21">
                    <id><![CDATA[21]]></id>
                </category>
                <category xlink:href="https://mywebsite/api/categories/14">
                    <id><![CDATA[14]]></id>
                </category>
                <category xlink:href="https://mywebsite/api/categories/33">
                    <id><![CDATA[33]]></id>
                </category>
            </categories>
            <products nodeType="product" api="products"/>
        </associations>
    </category>
    <category>
        <id><![CDATA[14]]></id>
        <id_parent xlink:href="https://mywebsite/api/categories/2"><![CDATA[2]]></id_parent>
        <level_depth><![CDATA[2]]></level_depth>
        <nb_products_recursive notFilterable="true"><![CDATA[14]]></nb_products_recursive>
        <active><![CDATA[1]]></active>
        <id_shop_default><![CDATA[1]]></id_shop_default>
        <is_root_category><![CDATA[0]]></is_root_category>
        <position><![CDATA[3]]></position>
        <date_add><![CDATA[2016-01-12 09:41:45]]></date_add>
        <date_upd><![CDATA[2016-02-12 10:20:54]]></date_upd>
        <name>
        <language id="1" xlink:href="https://mywebsite/api/languages/1"><![CDATA[Télécommande]]></language>
        <language id="3" xlink:href="https://mywebsite/api/languages/3"><![CDATA[Télécommande]]></language>
        <language id="4" xlink:href="https://mywebsite/api/languages/4"><![CDATA[Télécommande]]></language>
        </name>
        <associations>
            <categories nodeType="category" api="categories"/>
            <products nodeType="product" api="products"/>
        </associations>
    </category>
    <category>
        <id><![CDATA[15]]></id>
        <id_parent xlink:href="https://mywebsite/api/categories/2"><![CDATA[2]]></id_parent>
        <level_depth><![CDATA[2]]></level_depth>
        <nb_products_recursive notFilterable="true"><![CDATA[41]]></nb_products_recursive>
        <active><![CDATA[1]]></active>
        <id_shop_default><![CDATA[1]]></id_shop_default>
        <is_root_category><![CDATA[0]]></is_root_category>
        <position><![CDATA[1]]></position>
        <date_add><![CDATA[2016-01-13 15:34:14]]></date_add>
        <date_upd><![CDATA[2016-02-29 15:11:49]]></date_upd>
        <name>
            <language id="1" xlink:href="https://mywebsite/api/languages/1"><![CDATA[Audio-Vidéo]]></language>
            <language id="3" xlink:href="https://mywebsite/api/languages/3"><![CDATA[Audio-Video]]></language>
            <language id="4" xlink:href="https://mywebsite/api/languages/4"><![CDATA[Audio-Vidéo]]></language>
        </name>
        <associations>
            <categories nodeType="category" api="categories">
                <category xlink:href="https://mywebsite/api/categories/16">
                    <id><![CDATA[16]]></id>
                </category>
                <category xlink:href="https://mywebsite/api/categories/17">
                    <id><![CDATA[17]]></id>
                </category>
                <category xlink:href="https://mywebsite/api/categories/18">
                    <id><![CDATA[18]]></id>
                </category>
                <category xlink:href="https://mywebsite/api/categories/19">
                    <id><![CDATA[19]]></id>
                </category>
                <category xlink:href="https://mywebsite/api/categories/20">
                    <id><![CDATA[20]]></id>
                </category>
            </categories>
            <products nodeType="product" api="products"/>
        </associations>
    </category>
    <category>
        <id><![CDATA[19]]></id>
        <id_parent xlink:href="https://mywebsite/api/categories/15"><![CDATA[15]]></id_parent>
        <level_depth><![CDATA[3]]></level_depth>
        <nb_products_recursive notFilterable="true"><![CDATA[-1]]></nb_products_recursive>
        <active><![CDATA[1]]></active>
        <id_shop_default><![CDATA[1]]></id_shop_default>
        <is_root_category><![CDATA[0]]></is_root_category>
        <position><![CDATA[4]]></position>
        <date_add><![CDATA[2016-01-13 15:43:23]]></date_add>
        <date_upd><![CDATA[2016-02-29 15:13:45]]></date_upd>
        <name>
        <language id="1" xlink:href="https://mywebsite/api/languages/1"><![CDATA[Casque Yurbuds]]></language>
        <language id="3" xlink:href="https://mywebsite/api/languages/3"><![CDATA[Casque Yurbuds]]></language>
        <language id="4" xlink:href="https://mywebsite/api/languages/4"><![CDATA[Casque Yurbuds]]></language>
        </name>
        <associations>
            <categories nodeType="category" api="categories">
                <category xlink:href="https://mywebsite/api/categories/24">
                    <id><![CDATA[24]]></id>
                </category>
            </categories>
            <products nodeType="product" api="products"/>
        </associations>
    </category>
    <category>
        <id><![CDATA[21]]></id>
        <id_parent xlink:href="https://mywebsite/api/categories/2"><![CDATA[2]]></id_parent>
        <level_depth><![CDATA[2]]></level_depth>
        <nb_products_recursive notFilterable="true"><![CDATA[35]]></nb_products_recursive>
        <active><![CDATA[1]]></active>
        <id_shop_default><![CDATA[1]]></id_shop_default>
        <is_root_category><![CDATA[0]]></is_root_category>
        <position><![CDATA[2]]></position>
        <date_add><![CDATA[2016-01-13 15:46:00]]></date_add>
        <date_upd><![CDATA[2016-02-12 10:19:24]]></date_upd>
        <name>
        <language id="1" xlink:href="https://mywebsite/api/languages/1"><![CDATA[Multimédia]]></language>
        <language id="3" xlink:href="https://mywebsite/api/languages/3"><![CDATA[Multimedia]]></language>
        <language id="4" xlink:href="https://mywebsite/api/languages/4"><![CDATA[Multimédia]]></language>
        </name>
        <associations>
            <categories nodeType="category" api="categories">
                <category xlink:href="https://mywebsite/api/categories/26">
                    <id><![CDATA[26]]></id>
                </category>
                <category xlink:href="https://mywebsite/api/categories/28">
                    <id><![CDATA[28]]></id>
                </category>
            </categories>
            <products nodeType="product" api="products" />
        </associations>
    </category>
</categories>
</prestashop>

I could not change XML file format because it's generated by prestashop webservices. What I would like to do is to populate a JTree with all my categories inside the XML with hierarchical.

For example, here you have Firt one : "Racine". Under Racine, you have "Accueil". The result should be :

Racine
    Accueil
        Télécommande
        Audio-Vidéo
            Casque Yurbuds
        Multimédia

How could I do that ? Thanks


Solution

  • Found a solution.

    Create a class "MyTreeModel" which implements TreeModel. Inside this class, add an Object that represents my category. Get all childs of Categories and add to a HashMap with

    Contruct a JTree with MyTreeModel as parameter. Voilà