I have a following xml, with multiple levels( level 1, level 2, level 3). All the level 1 will go into the tab like showed in the picture.
<?xml version="1.0" encoding="UTF-8"?>
<treeNode label="projectTags" level="0">
<reference/>
<treeNode label="category" level="1">
<reference>
<dataField name="category_id" value="2"/>
<dataField name="labelFieldValue" value="IRIS"/>
<dataField name="category" value="IRIS"/>
<dataField name="labelFieldName" value="Category"/>
<dataField name="idFieldName" value="category_id"/>
<dataField name="idFieldValue" value="2"/>
</reference>
<treeNode label="project" level="2">
<reference>
<dataField name="category_id" value="2"/>
<dataField name="projectvisible" value="1"/>
<dataField name="project" value="Acenaphthene"/>
<dataField name="labelFieldValue" value="Acenaphthene"/>
<dataField name="labelFieldName" value="Project"/>
<dataField name="projectactive" value="1"/>
<dataField name="idFieldName" value="Project_id"/>
<dataField name="idFieldValue" value="1436"/>
<dataField name="project_id" value="1436"/>
<dataField name="casrn" value="83-32-9"/>
</reference>
</treeNode>
<treeNode label="project" level="2">
<reference>
<dataField name="category_id" value="2"/>
<dataField name="projectvisible" value="1"/>
<dataField name="project" value="Acetaldehyde"/>
<dataField name="labelFieldValue" value="Acetaldehyde"/>
<dataField name="labelFieldName" value="Project"/>
<dataField name="projectactive" value="1"/>
<dataField name="idFieldName" value="Project_id"/>
<dataField name="idFieldValue" value="37"/>
<dataField name="project_id" value="37"/>
<dataField name="casrn" value="75-07-0"/>
</reference>
<treeNode label="usage" level="3">
<reference>
<dataField name="usageVisible" value="1"/>
<dataField name="labelFieldValue" value="1991 IRIS"/>
<dataField name="usage_id" value="1158"/>
<dataField name="usageActive" value="1"/>
<dataField name="parent_usage" value=""/>
<dataField name="projectActive" value="1"/>
<dataField name="parent_usage_id" value=""/>
<dataField name="idFieldName" value="usage_id"/>
<dataField name="projectVisible" value="1"/>
<dataField name="isdefault" value="0"/>
<dataField name="usage" value="1991 IRIS"/>
<dataField name="description" value=""/>
<dataField name="labelFieldName" value="usage"/>
<dataField name="idFieldValue" value="1158"/>
<dataField name="project_id" value="37"/>
</reference>
</treeNode>
</treeNode>
</treeNode>
<treeNode label="category" level="1">
<reference>
<dataField name="category_id" value="1"/>
<dataField name="labelFieldValue" value="NAAQS"/>
<dataField name="category" value="NAAQS"/>
<dataField name="labelFieldName" value="Category"/>
<dataField name="idFieldName" value="category_id"/>
<dataField name="idFieldValue" value="1"/>
</reference>
<treeNode label="project" level="2">
<reference>
<dataField name="category_id" value="1"/>
<dataField name="projectvisible" value="1"/>
<dataField name="project" value="Aquatic Acidification"/>
<dataField name="labelFieldValue" value="Aquatic Acidification"/>
<dataField name="labelFieldName" value="Project"/>
<dataField name="projectactive" value="1"/>
<dataField name="idFieldName" value="Project_id"/>
<dataField name="idFieldValue" value="1576"/>
<dataField name="project_id" value="1576"/>
<dataField name="casrn" value=""/>
</reference>
<treeNode label="usage" level="3">
<reference>
<dataField name="usageVisible" value="1"/>
<dataField name="labelFieldValue" value="All refs"/>
<dataField name="usage_id" value="651"/>
<dataField name="usageActive" value="1"/>
<dataField name="parent_usage" value=""/>
<dataField name="projectActive" value="1"/>
<dataField name="parent_usage_id" value=""/>
<dataField name="idFieldName" value="usage_id"/>
<dataField name="projectVisible" value="1"/>
<dataField name="isdefault" value="0"/>
<dataField name="usage" value="All refs"/>
<dataField name="description" value=""/>
<dataField name="labelFieldName" value="usage"/>
<dataField name="idFieldValue" value="651"/>
<dataField name="project_id" value="1576"/>
</reference>
<treeNode label="usage" level="4">
<reference>
<dataField name="usageVisible" value="1"/>
<dataField name="labelFieldValue" value="Hydrology"/>
<dataField name="usage_id" value="652"/>
<dataField name="usageActive" value="1"/>
<dataField name="parent_usage" value="All refs"/>
<dataField name="projectActive" value="1"/>
<dataField name="parent_usage_id" value="651"/>
<dataField name="idFieldName" value="usage_id"/>
<dataField name="projectVisible" value="1"/>
<dataField name="isdefault" value="0"/>
<dataField name="usage" value="Hydrology"/>
<dataField name="description" value=""/>
<dataField name="labelFieldName" value="usage"/>
<dataField name="idFieldValue" value="652"/>
<dataField name="project_id" value="1576"/>
</reference>
</treeNode>
</treeNode>
</treeNode>
</treeNode>
</treeNode>
I need to parse it into JTree, to make it look like
I am using following code,
private DefaultMutableTreeNode buildTreeNode(Node rootNode)
{
DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(rootNode.getNodeName());
System.out.println("root\t" + rootNode.getNodeName() + "\t" + rootNode.getNodeValue());
if (rootNode.hasAttributes())
{
NamedNodeMap attributes = rootNode.getAttributes();
if (attributes.item(0).toString().contains("labelFieldValue"))
{
System.out.println(attributes.item(0).toString() + "\t" + attributes.item(1).toString());
String attr = attributes.item(1).toString();
treeNode.add(new DefaultMutableTreeNode("@" + attr));
}
}
if (rootNode.hasChildNodes())
{
NodeList children = rootNode.getChildNodes();
for (int i = 0; i < children.getLength(); i++)
{
Node node = children.item(i);
short nodeType = node.getNodeType();
if (nodeType == Node.ELEMENT_NODE)
{
treeNode.add(buildTreeNode(node));
}
else if (nodeType == Node.TEXT_NODE)
{
String text = node.getTextContent().trim();
if (!text.equals(""))
treeNode.add(new DefaultMutableTreeNode(text));
}
}
}
return treeNode;
}
How can I fix it?. I need to show only the values under "labelFieldValue", not everything.