1

I am parsing the data from XML & trying to set the tableview with the data from XML. Data I fetched from an XML file in JavaFX doesn't show up in TableView.

XML DOM Parser:

public class PopulatedDTOExamplesWithParsedXML {

public void main(String[] args) throws ParserConfigurationException, SAXException, IOException
{
    List<Countries> countries = parseCountriesXML();
}

public static List<Countries> parseCountriesXML() throws ParserConfigurationException,SAXException,IOException
{
    //Initialize a list of countries
    List<Countries> countries = new ArrayList();
    Countries countriess = null;

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new File("opendata.ecdc.europa.eu.xml")); ////The error caused by this line
    document.getDocumentElement().normalize();
    NodeList nList = document.getElementsByTagName("record");

    for (int temp = 0; temp < nList.getLength(); temp++)
    {
        Node node = nList.item(temp);
        if (node.getNodeType() == 1)
        {
            Element eElement = (Element) node;
            //Create new Country object
            countriess = new Countries();
            countriess.setGeoId(eElement.getAttribute("geoId"));
            countriess.setDate(eElement.getElementsByTagName("date").item(0).getTextContent());
            countriess.setCases(Integer.parseInt(eElement.getElementsByTagName("cases").item(0).getTextContent()));
            countriess.setDeaths(Integer.parseInt(eElement.getElementsByTagName("deaths").item(0).getTextContent()));
            countriess.setCountriesAndTerritories(eElement.getElementsByTagName("countriesAndTerritories").item(0).getTextContent());
            countriess.setCountryterritoryCode(eElement.getElementsByTagName("countryterritoryCode").item(0).getTextContent());
            countriess.setPopData(Integer.parseInt(eElement.getElementsByTagName("popData").item(0).getTextContent()));
            countriess.setContinentExp(eElement.getElementsByTagName("continentExp").item(0).getTextContent());

            //Add Countries to list
            countries.add(countriess);
        }
    }
    return countries;
}

} Parse Unknown XML Structure:

public class ParseUnknownXMLStructure {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
{
    //Get Document Builder
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    //Build Document
    Document document = builder.parse(new File("opendata.ecdc.europa.eu.xml"));

    //Normalize the XML Structure; It's just too important !!
    document.getDocumentElement().normalize();

    //Here comes the root node
    Element root = document.getDocumentElement();



    //Get all records
    NodeList nList = document.getElementsByTagName("record");


    visitChildNodes(nList);
}

//This function is called recursively
private static void visitChildNodes(NodeList nList)
{
    for (int temp = 0; temp < nList.getLength(); temp++)
    {
        Node node = nList.item(temp);
        if (node.getNodeType() == 1)
        {
            //Check all attributes
            if (node.hasAttributes()){
                //get attributes names and values
                NamedNodeMap nodeMap = node.getAttributes();
                for (int i = 0; i < nodeMap.getLength(); i++)
                {
                    Node tempNode = nodeMap.item(i);
                }
                if (node.hasChildNodes())
                {
                    //We got more childs; Let's visit them as well
                    visitChildNodes(node.getChildNodes());
                }
            }
        }
    }
}

} DataSource(POJO) File:

public class covidMain extends Application {
private TableView<Countries> table = new TableView<Countries>();
private ObservableList<Countries> data = FXCollections.observableArrayList();

final HBox hb = new HBox();
public static void main(String[] args) {launch(args);}
@Override
public void start(Stage stage) throws Exception {
    Scene scene = new Scene(new Group());
    stage.setTitle("Table View Covid-19");
    stage.setWidth(600);
    stage.setHeight(700);
    PopulatedDTOExamplesWithParsedXML parser = new PopulatedDTOExamplesWithParsedXML();
    File file = new File("opendata.ecdc.europa.eu.xml"); System.out.println(file.exists());
    data.setAll(parser.parseCountriesXML()); //The error caused by this line

    final Label label = new Label("Covid Cases");
    label.setFont(new Font("Arial",20));
    table.setEditable(true);

    TableColumn geoIdCol = new TableColumn("geoId");
    geoIdCol.setMinWidth(100);
    geoIdCol.setCellValueFactory(
            new PropertyValueFactory<Countries, String>("geoId"));

    TableColumn dateCol = new TableColumn("Date");
    dateCol.setMinWidth(100);
    dateCol.setCellValueFactory(
            new PropertyValueFactory<Countries, String>("date"));

    TableColumn casesCol = new TableColumn("Cases");
    casesCol.setMinWidth(100);
    casesCol.setCellValueFactory(
            new PropertyValueFactory<Countries, Integer>("cases"));

    TableColumn deathsCol = new TableColumn("Deaths");
    deathsCol.setMinWidth(100);
    deathsCol.setCellValueFactory(
            new PropertyValueFactory<Countries, Integer>("deaths"));

    TableColumn countriesAndTerritoriesCol = new TableColumn("Countries And Territories");
    countriesAndTerritoriesCol.setMinWidth(200);
    countriesAndTerritoriesCol.setCellValueFactory(
            new PropertyValueFactory<Countries, String>("countriesAndTerritories"));

    TableColumn countryterritoryCodeCol = new TableColumn("Countryterritory Code");
    countryterritoryCodeCol.setMinWidth(200);
    countryterritoryCodeCol.setCellValueFactory(
            new PropertyValueFactory<Countries, String>("countryterritoryCode"));

    TableColumn popDataCol = new TableColumn("Pop Data");
    popDataCol.setMinWidth(100);
    popDataCol.setCellValueFactory(
            new PropertyValueFactory<Countries, Integer>("popData"));

    TableColumn continentExpCol = new TableColumn("Continent Exp");
    continentExpCol.setMinWidth(200);
    continentExpCol.setCellValueFactory(
            new PropertyValueFactory<Countries, String>("continentExp"));

    table.setItems(data);

    table.getColumns().addAll(geoIdCol,dateCol,casesCol,deathsCol,countriesAndTerritoriesCol,countryterritoryCodeCol,popDataCol,continentExpCol);
    hb.setSpacing(3);

    final VBox vbox = new VBox();
    vbox.setSpacing(5);
    vbox.setPadding(new Insets(10, 0, 0, 10));
    vbox.getChildren().addAll(label, table, hb);

    ((Group) scene.getRoot()).getChildren().addAll(vbox);

    stage.setScene(scene);
    stage.show();
}

} And Countries Class:

public class Countries {
private String geoId;
private String date;
private Integer cases;
private Integer deaths;
private String countriesAndTerritories;
private String countryterritoryCode;
private Integer popData;
private String continentExp;

public String getGeoId() {return  geoId;}

public void setGeoId(String geoId) {this.geoId = geoId;}

public String getDate() {return date;}

public void setDate(String date) {this.date = date;}

public Integer getCases() {return  cases;}

public void setCases(Integer cases) {this.cases = cases;}

public Integer getDeaths() {return deaths;}

public void setDeaths(Integer deaths) {this.deaths = deaths;}

public String getCountriesAndTerritories() {return countriesAndTerritories;}

public void setCountriesAndTerritories(String countriesAndTerritories) {this.countriesAndTerritories = countriesAndTerritories;}

public String getCountryterritoryCode() {return countryterritoryCode;}

public void setCountryterritoryCode(String countryterritoryCode) {this.countryterritoryCode = countryterritoryCode;}

public Integer getPopData() {return  popData;}

public void setPopData(Integer popData) {this.popData = popData;}

public String getContinentExp() {return  continentExp;}

public void setContinentExp(String continentExp) { this.continentExp = continentExp;}

}

Error I get: enter image description here

Can you show me where I'm doing wrong?

0 Answers0