xml 到 java 中的 jtree 解析
xml to jtree parsing in java
我有一个关注 xml,有多个级别(1 级、2 级、3 级)。所有级别 1 都会进入如图所示的选项卡。
<?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>
我需要将其解析为 JTree,使其看起来像
我正在使用以下代码,
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;
}
但我得到了关注
我该如何解决?。我只需要显示 "labelFieldValue" 下的值,而不是所有内容。
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File f = new File("Elements2.xml");
Document d = builder.parse(f);
Node node = d.getDocumentElement();
DefaultMutableTreeNode parent = new DefaultMutableTreeNode();
parent = buildTreeNode(parent, node);
final DefaultMutableTreeNode finalnode = parent;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI(finalnode);
}
});
}
private static void createAndShowGUI(DefaultMutableTreeNode parent) {
final JFrame frame = new JFrame("Split Pane Example");
// Display the window.
frame.setSize(500, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set grid layout for the frame
frame.getContentPane().setLayout(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.addTab("IRIS", makePanel("IRIS", parent, 0));
tabbedPane.addTab("NAAQS", makePanel("NAAQS", parent, 1));
frame.getContentPane().add(tabbedPane);
}
private static JPanel makePanel(String text, DefaultMutableTreeNode parent, int i) {
JPanel p = new JPanel();
//p.add(new Label(text));
p.setLayout(new GridLayout(1, 1));
JTree jtree = new JTree(parent.getChildAt(i));
JScrollPane sp = new JScrollPane(jtree);
p.add(sp);
return p;
}
private static boolean specialCase = false;
private static DefaultMutableTreeNode buildTreeNode(DefaultMutableTreeNode parent, Node parentNode) {
DefaultMutableTreeNode currentLevel = null;
if (parentNode.getNodeName().equals("treeNode")) {
NodeList children = parentNode.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node node = children.item(i);
if (node.getNodeName().equals("reference") && !node.hasChildNodes()) {
currentLevel = new DefaultMutableTreeNode("root");
specialCase = true;
} else if (node.getNodeName().equals("reference") && node.hasChildNodes()) {
NodeList dataFields = node.getChildNodes();
for (int j = 0; j < dataFields.getLength(); j++) {
Node dataField = dataFields.item(j);
NamedNodeMap attributes = dataField.getAttributes();
if (attributes != null && attributes.item(0).toString().contains("labelFieldValue")) {
String attr = attributes.item(1).toString();
currentLevel = new DefaultMutableTreeNode("@"+attr);
parent.add(currentLevel);
break;
}
}
} else if (node.getNodeName().equals("treeNode") && node.hasChildNodes() && specialCase) {
specialCase = false;
parent = buildTreeNode(currentLevel, node);
} else if (node.getNodeName().equals("treeNode") && node.hasChildNodes() && !specialCase) {
currentLevel = buildTreeNode(currentLevel, node);
}
}
}
return parent;
}
我有一个关注 xml,有多个级别(1 级、2 级、3 级)。所有级别 1 都会进入如图所示的选项卡。
<?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>
我需要将其解析为 JTree,使其看起来像
我正在使用以下代码,
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;
}
但我得到了关注
我该如何解决?。我只需要显示 "labelFieldValue" 下的值,而不是所有内容。
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File f = new File("Elements2.xml");
Document d = builder.parse(f);
Node node = d.getDocumentElement();
DefaultMutableTreeNode parent = new DefaultMutableTreeNode();
parent = buildTreeNode(parent, node);
final DefaultMutableTreeNode finalnode = parent;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI(finalnode);
}
});
}
private static void createAndShowGUI(DefaultMutableTreeNode parent) {
final JFrame frame = new JFrame("Split Pane Example");
// Display the window.
frame.setSize(500, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set grid layout for the frame
frame.getContentPane().setLayout(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.addTab("IRIS", makePanel("IRIS", parent, 0));
tabbedPane.addTab("NAAQS", makePanel("NAAQS", parent, 1));
frame.getContentPane().add(tabbedPane);
}
private static JPanel makePanel(String text, DefaultMutableTreeNode parent, int i) {
JPanel p = new JPanel();
//p.add(new Label(text));
p.setLayout(new GridLayout(1, 1));
JTree jtree = new JTree(parent.getChildAt(i));
JScrollPane sp = new JScrollPane(jtree);
p.add(sp);
return p;
}
private static boolean specialCase = false;
private static DefaultMutableTreeNode buildTreeNode(DefaultMutableTreeNode parent, Node parentNode) {
DefaultMutableTreeNode currentLevel = null;
if (parentNode.getNodeName().equals("treeNode")) {
NodeList children = parentNode.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node node = children.item(i);
if (node.getNodeName().equals("reference") && !node.hasChildNodes()) {
currentLevel = new DefaultMutableTreeNode("root");
specialCase = true;
} else if (node.getNodeName().equals("reference") && node.hasChildNodes()) {
NodeList dataFields = node.getChildNodes();
for (int j = 0; j < dataFields.getLength(); j++) {
Node dataField = dataFields.item(j);
NamedNodeMap attributes = dataField.getAttributes();
if (attributes != null && attributes.item(0).toString().contains("labelFieldValue")) {
String attr = attributes.item(1).toString();
currentLevel = new DefaultMutableTreeNode("@"+attr);
parent.add(currentLevel);
break;
}
}
} else if (node.getNodeName().equals("treeNode") && node.hasChildNodes() && specialCase) {
specialCase = false;
parent = buildTreeNode(currentLevel, node);
} else if (node.getNodeName().equals("treeNode") && node.hasChildNodes() && !specialCase) {
currentLevel = buildTreeNode(currentLevel, node);
}
}
}
return parent;
}