我正在尝试通过解析 xml 文件动态加载 5 个组合框。根据在第一个组合框中选择的选项,我想更改其他四个组合框的值。您能否为此提出一些示例。
我浏览过,发现很少。但是,对我帮助不大。
代码片段如下。解析 xml 并加载到组合框
File fXmlFile = new File("c:\\DataXML.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList DeviceList = (NodeList) doc.getElementsByTagName("PICDevice");
int totaldevices = DeviceList.getLength();
System.out.println("Total no of devices : " + totaldevices);
for(int s=0; s<DeviceList.getLength() ; s++){
Node firstDeviceNode1 = DeviceList.item(s);
Element firstDeviceElement1 = (Element)firstDeviceNode1;
System.out.println("Device Name : " +
firstDeviceElement1.getAttribute("Description"));
NodeList firstCLCList1 = firstDeviceElement1.getElementsByTagName("CLCNum");
int totalCLCs1 = firstCLCList1.getLength();
System.out.println("Total no of CLCs : " + totalCLCs1);
for(int i=0; i<totalCLCs1 ; i++){
Element firstCLCElement = (Element)firstCLCList1.item(i);
System.out.println("CLC Name : " +
firstCLCElement.getAttribute("Description"));
String value = firstCLCElement.getAttribute("Description");
CLCcombo.addItem(value);
ArrayList<String> CLCelements = new ArrayList<String>();
//List<String> CLCelements = new ArrayList<String>();
boolean add = CLCelements.add(value);
// String[] CLCelements = firstCLCElement.getAttribute("Description");
}
}
System.out.println("-----------------------");
NodeList nList = doc.getElementsByTagName("CLCNum");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
Node node = nList.item(0);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String Mux1elements = (getTagValue("Mux1Inputs", eElement));
String Mux2elements = (getTagValue("Mux2Inputs", eElement));
String Mux3elements = (getTagValue("Mux3Inputs", eElement));
String Mux4elements = (getTagValue("Mux4Inputs", eElement));
String delims = "[,]+";
String[] Mux1tokens = Mux1elements.split(delims);
String[] Mux2tokens = Mux2elements.split(delims);
String[] Mux3tokens = Mux3elements.split(delims);
String[] Mux4tokens = Mux4elements.split(delims);
for (int i = 0; i < Mux1tokens.length; i++){
mux1combo.addItem(Mux1tokens[i]);
String[] mux1elements = Mux1tokens;
// mux1combo = mux1elements;
}
for (int i = 0; i < Mux2tokens.length; i++){
mux2combo.addItem(Mux2tokens[i]);
}
for (int i = 0; i < Mux3tokens.length; i++){
mux3combo.addItem(Mux3tokens[i]);
}
for (int i = 0; i < Mux4tokens.length; i++){
mux4combo.addItem(Mux4tokens[i]);
}
panel.add(mux1combo, BorderLayout.WEST);
panel.add(mux2combo, BorderLayout.WEST);
panel.add(mux3combo, BorderLayout.WEST);
panel.add(mux4combo, BorderLayout.WEST);
panel.add(CLCcombo, BorderLayout.WEST);
mux1combo.setEditable(true);
mux2combo.setEditable(true);
mux3combo.setEditable(true);
mux4combo.setEditable(true);
CLCcombo.setEditable(true);
// CLCcombo.addItemListener(new Sample_xml());
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
jlbLabel1 = new JLabel("Multiplexer1");
panel.add(jlbLabel1);
}
}
我不知道如何为此编写项目侦听器。你能帮我么。很抱歉,这个问题可能很简单。我是初学者,没有足够的时间学习。
谢谢你的帮助!!!