从JavaFX应用程序读取XML [英] Reading an XML from a JavaFX application
问题描述
我想通过我的 JavaFX
应用程序中的弹出窗口导入 XML
文件。导入后,我想阅读它。例如,我想存储每个< testbus>
< index>
和< tb_name>
在列表
或类似内容中,其中< index>
是列表
的索引
和< tb_name>
是 List
的元素
。我也希望每个< testbus>
我可以访问位域及其名称。所以我在想 List
List
。我找到了一个名为 JAXB
的java库,它可以解析 XML
文件,但我不知道如何实现我的目标希望。
I would like to import an XML
file via a pop-up window in my JavaFX
application. After the importing, I would like to read it. For example I want to store, for every <testbus>
the <index>
and the <tb_name>
in a List
or something similar, where the <index>
is the index
of the List
and the <tb_name>
is the element
of the List
. I also would like that for every <testbus>
I can access the bitfields and their name. So I was thinking about List
of List
. I have found a java Library called JAXB
that can parse XML
files, but I do not know how to achieve what I want.
这是 XML
文件
This is the XML
file
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<testbuses>
<testbus>
<index>00</index>
<tb_name>buffermngr00</tb_name>
<bitfield0>
<bitf_name>aaa_00</bitf_name>
</bitfield0>
<bitfield1>
<bitf_name>aaa_01</bitf_name>
</bitfield1>
<bitfield2>
<bitf_name>aaa_02</bitf_name>
</bitfield2>
<bitfield3>
<bitf_name>aaa_03</bitf_name>
</bitfield3>
<bitfield4>
<bitf_name>aaa_03</bitf_name>
</bitfield4>
<bitfield5>
<bitf_name>aaa_04</bitf_name>
</bitfield5>
<bitfield6>
<bitf_name>aaa_04</bitf_name>
</bitfield6>
<bitfield7>
<bitf_name>aaa_05</bitf_name>
</bitfield7>
</testbus>
<testbus>
<index>01</index>
<tb_name>buffermngr01</tb_name>
<bitfield0>
<bitf_name>bbb_00</bitf_name>
</bitfield0>
<bitfield1>
<bitf_name>bbb_00</bitf_name>
</bitfield1>
<bitfield2>
<bitf_name>bbb_00</bitf_name>
</bitfield2>
<bitfield3>
<bitf_name>bbb_00</bitf_name>
</bitfield3>
<bitfield4>
<bitf_name>bbb_01</bitf_name>
</bitfield4>
<bitfield5>
<bitf_name>bbb_01</bitf_name>
</bitfield5>
<bitfield6>
<bitf_name>bbb_02</bitf_name>
</bitfield6>
<bitfield7>
<bitf_name>bbb_03</bitf_name>
</bitfield7>
</testbus>
</testbuses>
无论如何,这个XML的结构并不严格,所以如果你有一个更好的结构建议为了方便阅读,我很乐意听取你的解决方案。
Anyway the structure of this XML is not strict, so if you have a suggestion for a better structure in order to make the reading easily, I will be happy to hear your solution.
推荐答案
对于你在XML中提供的xml。 br>
首先创建一个java POJO类,其字段为:
For your xml provided in the XML.
First create a java POJO class with fields as:
String index;
String tb_name;
List<String> bitf_names;
使用下面的课程:
import java.util.List;
class TestBus {
private String index;
private String tb_name;
private List<String> bitf_names;
public String getIndex() {
return index;
}
public void setIndex(String index) {
this.index = index;
}
public String getTb_name() {
return tb_name;
}
public void setTb_name(String tb_name) {
this.tb_name = tb_name;
}
public List<String> getBitf_names() {
return bitf_names;
}
public void setBitf_names(List<String> bitf_name) {
this.bitf_names = bitf_name;
}
@Override
public String toString() {
return "TestBus [index=" + index + ", tb_name=" + tb_name + ", bitf_name=" + bitf_names + "]";
}
}
之后,请使用以下代码创建列表TestBus类:
ie List< TestBus> testBusList = new ArrayList<>();
After that, use the code below to create a list of TestBus classes:
i.e List<TestBus> testBusList = new ArrayList<>();
将此类用于完整的代码和逻辑:
Use this class for the complete code and logic:
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ReadXMLFile {
public static List<TestBus> testBuses = new ArrayList<>();
public static void main(String argv[]) {
try {
File fXmlFile = new File("D:\\ECLIPSE-WORKSPACE\\demo-xml-project\\src\\main\\resources\\testbus.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList testBusNodeList = doc.getElementsByTagName("testbus");
for (int parameter = 0; parameter < testBusNodeList.getLength(); parameter++) {
TestBus testBus = new TestBus();
Node node = testBusNodeList.item(parameter);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
String index = eElement.getElementsByTagName("index").item(0).getTextContent();
String tb_name = eElement.getElementsByTagName("tb_name").item(0).getTextContent();
NodeList bitf_name = eElement.getElementsByTagName("bitf_name");
List<String> bitf_namesList = new ArrayList<>();
IntStream.range(0, bitf_name.getLength()).forEach(bName -> {
bitf_namesList.add(bitf_name.item(bName).getTextContent());
});
testBus.setIndex(index);
testBus.setTb_name(tb_name);
testBus.setBitf_names(bitf_namesList);
testBuses.add(testBus);
}
}
} catch (Exception e) {
System.out.println("!!!!!!!! Exception while reading xml file :" + e.getMessage());
}
testBuses.forEach(bus -> System.out.println(bus)); // in single line
System.out.println("###################################################");
// using getters
testBuses.forEach(bus -> {
System.out.println("index = " + bus.getIndex());
System.out.println("tb_name = " + bus.getTb_name());
System.out.println("bitf_names = " + bus.getBitf_names());
System.out.println("#####################################################");
});
}
}
这篇关于从JavaFX应用程序读取XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!