Set XML values in jTable

1.5k views Asked by At

I've got a jTable and now I want to add the content of a XML-File into it. I dont know how I should browse through the XML-File, I was currently only able to manipulate the data (rewrite, deleting) and to get the Values of a Single tag and not of all. I'm using JDOM

my XML-File looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<app>
  <!-- Pfad für Templates und speicher ort für die Entity.java-->
  <Entity>
    <Name>Aposting</Name>
    <Art>0</Art>
    <Datum>Wed Nov 19 10:51:10 CET 2014</Datum>
  </Entity>

  <Entity>
    <Name>Aposting</Name>
    <Art>1</Art>
    <Datum>Wed Nov 19 11:30:34 CET 2014</Datum>
  </Entity>

  <Entity>
    <Name>Aposting</Name>
    <Art>1</Art>
    <Datum>Wed Nov 19 15:21:12 CET 2014</Datum>
  </Entity>

  <Entity>
    <Name>Aposting</Name>
    <Art>2</Art>
    <Datum>Thu Nov 20 8:01:10 CET 2014</Datum>
  </Entity>

</app>

And i want to display it in a Table like this:

enter image description here

Thank you for your help

Edit:

i've made a Custom model

package TableTest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

/**
 *
 * @author Administrator
 */
public class ModelData implements TableModel {

    List<TableData> data = new ArrayList<>();
    String colNames[] = {"Name", "Type", "Date"};
    Class<?> colClasses[] = {String.class, String.class, Date.class};

    ModelData() {
        data.add(new TableData("Aposting", "Created", new Date()));
        data.add(new TableData("Aposting", "Edited", new Date()));
        data.add(new TableData("Aposting", "Edited", new Date()));
        data.add(new TableData("Aposting", "Deleted", new Date()));
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return colNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (columnIndex == 0) {
            return data.get(rowIndex).getName();
        }
        if (columnIndex == 1) {
            return data.get(rowIndex).getType();
        }
        if (columnIndex == 2) {
            return data.get(rowIndex).getDate();
        }
        return null;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return colNames[columnIndex];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return colClasses[columnIndex];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

        if (columnIndex == 0) {
            data.get(rowIndex).setName((String) aValue);
        }
        if (columnIndex == 1) {
            data.get(rowIndex).setType((String) aValue);
        }
        if (columnIndex == 2) {
            data.get(rowIndex).setDate((Date) aValue);
        }
    }

    @Override
    public void addTableModelListener(TableModelListener l) {

    }

    @Override
    public void removeTableModelListener(TableModelListener l) {

    }
}

and a TableData class which is to access:

package TableTest;

import java.util.Date;

public class TableData {

    String name;
    String type;
    Date date;

    public TableData(String name, String type, Date date) {
        super();
        this.name = name;
        this.type = type;
        this.date = date;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Date getDate() {
        return date;
    }

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

}

and it works with the added values in "ModelData" but i still got no clue how i could extract all Entities out of my XML-File

1

There are 1 answers

0
trashgod On BEST ANSWER

You'll need two things for this:

Depending on the chosen parser, you can either

  • Construct a Java data structure, e.g. List<Entity>, that can be accessed in the TableModel.

  • Access the document object model directly to meet the TableModel contract for getValueAt(), getRowCount(), etc.

Addendum: Based on your edit,

  • While you evaluate other parsing options, start with a simple SAXParser, illustrated here. The example builds a List<School>; you'll build a List<TableData>.

  • In your custom table model, extend AbstractTableModel to get the event handling illustrated here.