我不熟悉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 webservices生成的。 我想要做的是使用层次结构在XML中填充我的所有类别的JTree。
例如,这里有第一个:“拉辛”。在拉辛下,你有“Accueil”。结果应该是:
Racine
Accueil
Télécommande
Audio-Vidéo
Casque Yurbuds
Multimédia
我怎么能这样做? 感谢
答案 0 :(得分:0)
找到解决方案。
创建一个实现TreeModel的类“MyTreeModel”。 在此类中,添加一个表示我的类别的Object。 获取所有类别的子项并使用
添加到HashMap使用MyTreeModel构造一个JTree作为参数。瞧