从JavaFX应用程序读取XML

时间:2018-07-26 09:59:17

标签: java xml xml-parsing jaxb

我想通过我的XML应用程序中的弹出窗口导入JavaFX文件。导入后,我想阅读它。例如,我想将<testbus><index>中的每个<tb_name>List存储在<index>或类似的东西中,其中index是{{1 List<tb_name>中的}}是element中的List。我也希望每个<testbus>都可以访问位域及其名称。所以我在考虑List中的List。我发现了一个名为JAXB的Java库,可以解析XML文件,但是我不知道如何实现我想要的。

这是XML文件

<?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的结构都不严格,因此,如果您有一个更好的结构建议以便于阅读,我将很高兴听到您的解决方案。

1 个答案:

答案 0 :(得分:2)

用于XML中提供的xml。
首先创建一个具有以下字段的Java POJO类:

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类的列表:
List<TestBus> testBusList = new ArrayList<>();

将此类用于完整的代码和逻辑:

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("#####################################################");
        });
    }
}