Reputation: 69
I managed to create an XML file using JAXB with the following output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<data>
<id>1000</id>
<age>10</age>
<values>
<value>646.2</value>
<value>637.44</value>
</values>
<dates>
<date>2014-06-05</date>
<date>2014-06-04</date>
</dates>
</data>
<data>
<id>1001</id>
<age>10</age>
<values>
<value>546.4</value>
<value>541.5</value>
</values>
<dates>
<date>2014-06-05</date>
<date>2014-06-04</date>
</dates>
</data>
<data>
<id>1002</id>
<age>12</age>
<values>
<value>40.59</value>
<value>40.21</value>
</values>
<dates>
<date>2014-06-05</date>
<date>2014-06-04</date>
</dates>
</data>
</person>
My person class:
public class Person {
private final SimpleStringProperty id = new SimpleStringProperty();
private final SimpleIntegerProperty age = new SimpleIntegerProperty();
private List<Double> value = new ArrayList<Double>();
private List<String> date = new ArrayList<String>();
public String getId() {
return id.get();
}
@XmlElement(name = "id")
public void setId(String id) {
this.id.set(id);
}
public Integer getAge() {
return age.get();
}
@XmlElement(name = "age")
public void setAge(Integer age) {
this.age.set(age);
}
@XmlElementWrapper(name = "values")
@XmlElement(name = "value")
public List<Double> getValue() {
return value;
}
public void setValue(List<Double> value) {
this.value = value;
}
@XmlElementWrapper(name = "dates")
@XmlElement(name = "date")
public List<String> getDates() {
return date;
}
public void setDates(List<String> date) {
this.date = date;
}
}
My list wrapper class (as no annotation allowed for observable list):
@XmlRootElement(name = “person”)
public class ListWrapper {
private List<Person> persons;
public PersonListWrapper() {
persons = FXCollections.<Person>observableArrayList();
}
public void setPerson(List<Person> persons) {
this.persons = persons;
}
@XmlElement(name = “data”)
public List<Person> getPerson() {
return this.persons;
}
}
Parts of my main app, incl. unmarshalling method:
private ObservableList<Person> pData = FXCollections.observableArrayList();
.
public ObservableList<Person> getPersonData() {
return this.pData;
}
.
public void loadFile (File file) {
JAXBContext jaxb = JAXBContext.newInstance(ListWrapper.class);
Unmarshaller um = jaxb.createUnmarshaller();
ListWrapper wr = (ListWrapper) um.unmarshal(file);
pData.clear();
pData.addAll(wr.getPerson());
}
Parts of my javafx controller class:
public void initializePersonTable() {
idCol.setCellValueFactory(new PropertyValueFactory<Person, String>("id"));
ageCol.setCellValueFactory(new PropertyValueFactory<Person, Integer>("age"));
}
.
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
this.personTable.setItems(mainApp.getPersonData());
}
.
private void NewPersonButton(ActionEvent event) {
Person p = new Person();
p.setSymbol(personTF.getText()); // personTF = JavaFX Textfield
p.setWeight(Integer.parseInt(ageTF.getText())); // ageTF = JavaFX Textfield
mainApp.getPersonData().add(p);
}
When I load a saved XML file, the "id" and "age" are being added to a tableview via the observable list. How can I access the "value" and "date" data after loading the file? I want to use the data to create statistics.
If I eg. use:
Person p = new Person();
System.out.println(p.getDates());
... I just get an empty List returned
Upvotes: 2
Views: 658
Reputation: 69
greenhorn problems... I found a solution myself:
private void Test{
for (Person p : personTable.getItems()) {
System.out.println(p.getDates());
System.out.println(p.getValues());
}
Upvotes: 2