问题
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 elementof 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.
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>
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.
回答1:
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;
Use the class below for that:
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 + "]";
}
}
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("#####################################################");
});
}
}
来源:https://stackoverflow.com/questions/51536150/reading-an-xml-from-a-javafx-application