gpt4 book ai didi

java - 如何使用 xml 文件中的数据创建 GUI?

转载 作者:行者123 更新时间:2023-11-30 08:13:49 25 4
gpt4 key购买 nike

我想要的是创建一个由一组按钮组成的动态 GUI(在我的例子中,每个按钮代表 xml 文件中的一个元素),当我单击一个按钮时,将创建一个代表其子元素的按钮..我不知道是否可以使用 DOM 或 JAXB 来做到这一点..如果有人可以帮助我或有其他想法,我将非常感激!谢谢!!

  <?xml version="1.0" encoding="UTF-8" ?> 

- <HWData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="hw_data.xsd">

<Header time="2015-02-03T02:02:56" uploaded="false" version="1.0" />

- <NE MOID="NE-SGSN-387826" objectClass="NE" objectClassVersion="1" NEId="387826" NEType="SGSN" systemTitle="SGSN-MNB-" locationName="MANOUBA" systemReleaseVersion="J8" softwareReleaseVersion="J8 50.70-0 50" serialNumber="387826" vendorName="Nokia">

- <EQHO MOID="NE-SGSN-387826/DN:EQHO-1A" objectClass="EQHO" objectClassVersion="1" equipmentHolderId="11" equipmentHolderType="Cabinet" equipmentHolderSpecificType="EC217_A" locationName="1A" serialNumber="" interchangeability="" version="" identificationCode="" vendorName="Nokia">

- <EQHO MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-1-0" objectClass="EQHO" objectClassVersion="1" equipmentHolderId="11100" equipmentHolderType="Cartridge" equipmentHolderSpecificType="ACH16_A" locationName="1A001-00" serialNumber="XY131403380" interchangeability="" version="" identificationCode="" vendorName="Nokia">

<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-1-0/UNIT-1-PIU-1" objectClass="UNIT" objectClassVersion="1" unitId="1" unitTypeActual="ACPI4_A" unitTypeExpected="ACPI4_A" position="1" operationalState="enabled" serialNumber="FK124600007" interchangeability="B" version="3C" identificationCode="C111969" vendorName="Nokia" />

<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-1-0/UNIT-1-RTM-1" objectClass="UNIT" objectClassVersion="1" unitId="1" unitTypeActual="CPRT4_A" unitTypeExpected="CPRT4_A" position="1" operationalState="enabled" serialNumber="JG130400410" interchangeability="A" version="2A" identificationCode="C111970" vendorName="Nokia" />

<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-1-0/UNIT-FAN-2" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400960" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />

<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-1-0/UNIT-FAN-1" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400980" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />

</EQHO>

<EQHO MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-1-0" objectClass="EQHO" objectClassVersion="1" equipmentHolderId="111000501" equipmentHolderType="Cartridge" equipmentHolderSpecificType="CHAF2_A" locationName="1A001-00-FFI-1" serialNumber="31401904" interchangeability="A" version="" identificationCode="C110639" vendorName="Nokia" />

- <EQHO MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-2-0" objectClass="EQHO" objectClassVersion="1" equipmentHolderId="11200" equipmentHolderType="Cartridge" equipmentHolderSpecificType="ACH16_A" locationName="1A002-00" serialNumber="XY131403370" interchangeability="" version="" identificationCode="" vendorName="Nokia">

<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-2-0/UNIT-1-PIU-1" objectClass="UNIT" objectClassVersion="1" unitId="1" unitTypeActual="ACPI4_A" unitTypeExpected="ACPI4_A" position="1" operationalState="enabled" serialNumber="FK124600059" interchangeability="B" version="3C" identificationCode="C111969" vendorName="Nokia" />

<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-2-0/UNIT-SHM-2" objectClass="UNIT" objectClassVersion="1" unitId="3" unitTypeActual="ASMGR_A" unitTypeExpected="ASMGR_A" position="3" operationalState="enabled" serialNumber="XY131402619" interchangeability="A" version="4B" identificationCode="C110581" vendorName="Nokia" />

<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-2-0/UNIT-FAN-3" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400977" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />

<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-2-0/UNIT-FAN-1" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400971" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />

<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-2-0/UNIT-FAN-2" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400975" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />

</EQHO>

<EQHO MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-2-0" objectClass="EQHO" objectClassVersion="1" equipmentHolderId="112000502" equipmentHolderType="Cartridge" equipmentHolderSpecificType="CHAF2_A" locationName="1A002-00-FFI-2" serialNumber="31401893" interchangeability="A" version="" identificationCode="C110639" vendorName="Nokia" />

- <EQHO MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-3-0" objectClass="EQHO" objectClassVersion="1" equipmentHolderId="11300" equipmentHolderType="Cartridge" equipmentHolderSpecificType="ACH16_A" locationName="1A003-00" serialNumber="XY131403377" interchangeability="" version="" identificationCode="" vendorName="Nokia">

<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-3-0/UNIT-SHM-1" objectClass="UNIT" objectClassVersion="1" unitId="3" unitTypeActual="ASMGR_A" unitTypeExpected="ASMGR_A" position="3" operationalState="enabled" serialNumber="XY131301773" interchangeability="A" version="4B" identificationCode="C110581" vendorName="Nokia" />

<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-3-0/UNIT-FAN-1" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400963" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />

<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-3-0/UNIT-FAN-2" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400962" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />

<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-3-0/UNIT-FAN-3" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400988" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />

</EQHO>

<EQHO MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-3-0" objectClass="EQHO" objectClassVersion="1" equipmentHolderId="113000503" equipmentHolderType="Cartridge" equipmentHolderSpecificType="CHAF2_A" locationName="1A003-00-FFI-3" serialNumber="31401903" interchangeability="A" version="" identificationCode="C110639" vendorName="Nokia" />

</EQHO>

</NE>

</HWData>

最佳答案

删除了之前基于 XStream 的答案,支持 JAXB。

将 jaxb2-maven-plugin 添加到您的 pom.xml 中:

        <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>src/main/resources/xsd/</schemaDirectory>
</configuration>
</plugin>

把你的XSD放在src/main/resources/xsd/

类 HwDataTree 扩展 DynamicTreeDemo这是 Oracle 教程的一部分。下载这两个类并将它们添加到您的项目中。

import generated.EQHO;
import generated.HWData;
import generated.NE;
import generated.UNIT;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.util.List;

public class HwDataTree extends DynamicTreeDemo {

// the path of your XML file
private static final String PATH = "C:/example.xml";

@Override
public void populateTree(DynamicTree treePanel) {
HWData hwData = initData();
treePanel.addObject(null, formatHeader(hwData));
treePanel.addObject(null, formatNE(hwData));
addNodesRecursive(hwData.getNE(), null, treePanel);
}

private HWData initData() {
try {
File xml = new File(PATH);
JAXBContext jaxbContext = JAXBContext.newInstance(HWData.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (HWData) jaxbUnmarshaller.unmarshal(xml);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private void addNodesRecursive(NE parent, DefaultMutableTreeNode parentNode, DynamicTree treePanel) {
List<EQHO> children = parent.getEQHO();
for (EQHO child : children) {
DefaultMutableTreeNode childNode = treePanel.addObject(parentNode, formatEQHO(child));
addNodesRecursive(child, childNode, treePanel);
}
}

private void addNodesRecursive(EQHO parent, DefaultMutableTreeNode parentNode, DynamicTree treePanel) {
List<Object> children = parent.getEQHOAndUNIT();
for (Object child : children) {
DefaultMutableTreeNode childNode = treePanel.addObject(parentNode, getMOID(child));
if (child instanceof EQHO) {
EQHO eqho = (EQHO) child;
addNodesRecursive(eqho, childNode, treePanel);
}
}
}

private String getMOID(Object child) {
String moid;
if (child instanceof EQHO) {
EQHO eqho = (EQHO) child;
moid = formatEQHO(eqho);
} else if (child instanceof UNIT) {
UNIT unit = (UNIT) child;
moid = formatUnit(unit);
} else {
throw new RuntimeException("Could not parse type of: " + child);
}
return moid;
}

private String formatHeader(HWData hwData) {
return String.format("Header time=%s", hwData.getHeader().getTime());
}

private String formatNE(HWData hwData) {
return String.format("NE MOID=%s", hwData.getNE().getMOID());
}

private String formatEQHO(EQHO child) {
return String.format("EQHO MOID=%s", child.getMOID());
}

private String formatUnit(UNIT unit) {
return String.format("UNIT MOID=%s", unit.getMOID());
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

private static void createAndShowGUI() {
JFrame frame = new JFrame("Tree");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
HwDataTree newContentPane = new HwDataTree();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
}

通过执行 mvn clean compile 生成包装器。

然后运行HwDataTree,结果是这样的:

enter image description here

关于java - 如何使用 xml 文件中的数据创建 GUI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29795821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com