1

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.

MPA
  • 355
  • 3
  • 14
  • Do you try to read a documentation on how to use it? Google it, you will find something. – Harry Coder Jul 26 '18 at 10:32
  • yeah I have tried, but maybe you can tell me if JAXB is a correct option and also if the structure of the XML suits my needs. Thank you – MPA Jul 26 '18 at 11:17
  • 1
    Refer to this link : https://stackoverflow.com/questions/5059224/which-is-the-best-library-for-xml-parsing-in-java . JAXB annotation in used by default with JPA Entities to serialize objects. I have never used it to directly read XML data, but it should be able to do so. The link above is a discussion about which Java XML library you can use. Hope it will help. – Harry Coder Jul 26 '18 at 11:48

1 Answers1

2

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("#####################################################");
        });
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
dangi13
  • 1,275
  • 1
  • 8
  • 11
  • 1
    Thank you very much. Now the reading works really good. Let's say I would like to populate a `ComboBox` with the `Tb_name`s. Now I am using an `ObservableList` to populate that controller, but I would like to populate it via `XML` reading. This should not be a problem, but my aim is also to populate some Labels with the right `Bitf_names` depending on which `Tb_name` is selected via the `ComboBox`. Do you have any idea? – MPA Jul 27 '18 at 09:17
  • If it helped you can you please mark it as an accepted answer. For your ComboBox question i didn't got you properly as what exactly do you want. – dangi13 Jul 27 '18 at 09:25
  • I have marked it, sorry! Anyway I will open another question. – MPA Jul 27 '18 at 09:27