我想在中的节点名称旁边显示属性的名称和值 JTree
. 有人能告诉我怎么做吗?下面是我用来显示 JTree
:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Event.*;
import java.io.*;
import javax.swing.tree.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.apache.xerces.parsers.*;
public class XMLTreeView {
private SAXTreeBuilder saxTree = null;
private static String file = "";
public static void main(String args[]) {
JFrame frame = new JFrame("XMLTreeView: [ games.xml ]");
frame.setSize(400, 400);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
});
file = "example1.xml";
new XMLTreeView(frame);
}
public XMLTreeView(JFrame frame) {
frame.getContentPane().setLayout(new BorderLayout());
DefaultMutableTreeNode top = new DefaultMutableTreeNode(file);
// DefaultMutableTreeNode top = new DefaultMutableTreeNode("XML Document");
saxTree = new SAXTreeBuilder(top);
try {
SAXParser saxParser = new SAXParser();
saxParser.setContentHandler(saxTree);
saxParser.parse(new InputSource(new FileInputStream(file)));
} catch (Exception ex) {
top.add(new DefaultMutableTreeNode(ex.getMessage()));
}
JTree tree = new JTree(saxTree.getTree());
JScrollPane scrollPane = new JScrollPane(tree);
frame.getContentPane().add("Center", scrollPane);
frame.setVisible(true);
}
}
class SAXTreeBuilder extends DefaultHandler {
private DefaultMutableTreeNode currentNode = null;
private DefaultMutableTreeNode previousNode = null;
private DefaultMutableTreeNode rootNode = null;
public SAXTreeBuilder(DefaultMutableTreeNode root) {
rootNode = root;
}
public void startDocument() {
currentNode = rootNode;
}
public void endDocument() {
}
public void characters(char[] data, int start, int end) {
String str = new String(data, start, end);
if (!str.equals("") && Character.isLetter(str.charAt(0))) {
currentNode.add(new DefaultMutableTreeNode(str));
}
}
public void startElement(String uri, String qName, String lName, Attributes atts) {
previousNode = currentNode;
currentNode = new DefaultMutableTreeNode(lName);
// Add attributes as child nodes //
attachAttributeList(currentNode, atts);
previousNode.add(currentNode);
}
public void endElement(String uri, String qName, String lName) {
if (currentNode.getUserObject().equals(lName)) {
currentNode = (DefaultMutableTreeNode) currentNode.getParent();
}
}
public DefaultMutableTreeNode getTree() {
return rootNode;
}
private void attachAttributeList(DefaultMutableTreeNode node, Attributes atts) {
for (int i = 0; i < atts.getLength(); i++) {
String name = atts.getLocalName(i);
String value = atts.getValue(name);
node.add(new DefaultMutableTreeNode(name + " = " + value));
}
}
}
在 JTree
,我想显示 NEType
属性旁边 NE
节点,以及 equipmentHolderType
属性旁边 EQHO
节点
NE NEType=WBTS
EQHO equipmentHolderType=Subrack
我想要这样的东西:
下面是我处理的xml文件示例:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>
<HWData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="hw_data.xsd">
<Header xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" AdaptationName="NWI3BulkUpload" AccessProtocol="NWI3" time="2004-01-01T00:04:02" uploaded="true" version="1.0" />
<NE xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" vendorName="Nokia Siemens Networks" objectClass="NE" objectClassVersion="1" MOID="NE-RNC-1/DN:NE-WBTS-4183" NEId="PLMN-PLMN/RNC-1/WBTS-4183" NEType="WBTS" operationalState="enabled" locationName="Tun4183" nameFromPlanningSystem="" systemTitle="Nokia Flexi WCDMA Base Station">
<EQHO vendorName="Nokia Siemens Networks" objectClass="EQHO" objectClassVersion="1" MOID="NE-RNC-1/DN:NE-WBTS-4183/EQHO-40448" equipmentHolderId="40448" equipmentHolderType="Subrack" equipmentHolderSpecificType="472100A-40448" identificationCode="472100A" version="101" serialNumber="K9111641678" userLabel="FRGP" state="working">
<UNIT vendorName="Nokia Siemens Networks" objectClass="UNIT" objectClassVersion="1" MOID="NE-RNC-1/DN:NE-WBTS-4183/EQHO-40448/UNIT-158" unitId="158" unitTypeActual="CORE_FRGP" identificationCode="084629A" version="101" serialNumber="K9111641678" />
</EQHO>
<EQHO vendorName="Nokia Siemens Networks" objectClass="EQHO" objectClassVersion="1" MOID="NE-RNC-1/DN:NE-WBTS-4183/EQHO-173" equipmentHolderId="173" equipmentHolderType="Subrack" equipmentHolderSpecificType="471469A-173" identificationCode="471469A" version="" serialNumber="L1104816112" userLabel="FSME" state="working">
<UNIT vendorName="N" objectClass="UNIT" objectClassVersion="1" MOID="NE-RNC-1/DN:NE-WBTS-4183/EQHO-173/UNIT-16" unitId="16" unitTypeActual="CORE_FSME" identificationCode="083833A" version="104" serialNumber="L1104816112" />
<UNIT vendorName="NOKIA SIEMENS NETWORKS" objectClass="UNIT" objectClassVersion="1" MOID="NE-RNC-1/DN:NE-WBTS-4183/EQHO-173/UNIT-225" unitId="225" unitTypeActual="FTLB" identificationCode="471984A" version="103" serialNumber="RY104807867" />
</EQHO>
<EQHO vendorName="NSN" objectClass="EQHO" objectClassVersion="1" MOID="NE-RNC-1/DN:NE-WBTS-4183/EQHO-40192" equipmentHolderId="40192" equipmentHolderType="Subrack" equipmentHolderSpecificType="472083A-40192" identificationCode="472083A" version="101" serialNumber="L6105220714" userLabel="FXDA" state="working" />
</NE>
</HWData>
2条答案
按热度按时间v9tzhpje1#
您可以更改用于构造树节点的字符串:
这是我运行的完整代码:
这是我使用的example2.xml文件:
这是运行程序的屏幕截图:
oo7oh9g92#
在下面的变体中,我使用了
javax.xml.parsers
,一个修改的getNodeText()
以及你的最新xml
.XMLTreeView.java
: