<?xml version="1.0" encoding="utf-8"?>
<items>
<item>
<name>
<digital>
<mobile>
</mobile>
<computer>
</computer>
</digital>
<home>
</home>
</name>
</item>
</items>
XML文件
public HashMap getData(NodeList categoryList){
if(categoryList.getLength() == 1 && categoryList.item(0).getNodeName() == "#text")
return null;
HashMap al = new HashMap();
for (int i=0; i < categoryList.getLength(); i++)
{
if(categoryList.item(i) instanceof Element == false) continue;
String strOld = categoryList.item(i).getNodeName();
String strNew = changeTag(categoryList.item(i));
String str = categoryList.item(i).getNodeName().replace(strOld, strNew);
al.put(str, getData(categoryList.item(i).getChildNodes()));
}
return al;
}
我从XML文件获取数据并存储在hashmap中。
HashMap categoryData = new HashMap();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlFile);
document.getDocumentElement().normalize();
NodeList node = document.getElementsByTagName("name");
NodeList categoryList = node.item(0).getChildNodes();
categoryData = getData(categoryList);
我想以层次结构JList
显示这些哈希数据。
如果我在第一个列表上单击数字,则第二个列表显示移动和计算机。
如何为JList
设置模型?