Java JTree / Prestashop 类别

Java JTree / Prestashop categories

我不熟悉 JTree 组件和 XML。 我有一个 XML 文件,代表 Prestashop 电子商务平台中的类别:

<?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>

我无法更改 XML 文件格式,因为它是由 prestashop 网络服务生成的。 我想做的是用层次结构 XML 中的所有类别填充 JTree。

例如,这里有第一个:"Racine"。在拉辛下,你有 "Accueil"。结果应该是:

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

我该怎么做? 谢谢

找到解决方案。

创建一个 class "MyTreeModel" 来实现 TreeModel。 在此 class 中,添加一个代表我的类别的对象。 获取 Categories 的所有子项并使用

添加到 HashMap

以 MyTreeModel 作为参数构造一个 JTree。瞧