0
我想解開一個我打開的XML文件,但它在我打印時返回 「[[email protected]]」。JAXB unmarshal XML文件ObservableList
我看着這裏,但它不工作Null Pointer Exception in JAXB RI ClassFactory
類問題:
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Problem {
private StringProperty problems;
public Problem() {
}
public Problem(String problems) {
this.problems = new SimpleStringProperty(problems);
}
public String getproblems() {
return problems.get();
}
public void setproblems(String problems) {
this.problems.set(problems);
}
public StringProperty problemsProperty() {
return problems;
}
@Override
public String toString(){return getproblems();}
}
包裝:
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javafx.collections.ObservableList;
@XmlRootElement(name = "probl")
public class ProblemListWrapper {
private ObservableList<Problem> problem;
@XmlJavaTypeAdapter(ListProblemAdapter.class)
public ObservableList<Problem> getProblems() {
return problem;
}
public void setProblems(ObservableList<Problem> problems) {
this.problem = problems;
}
}
ListAdapter:
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class ListProblemAdapter extends XmlAdapter<ListProblem, ObservableList<Problem>> {
@Override
public ObservableList<Problem> unmarshal(ListProblem v) throws Exception {
ObservableList<Problem> problemData = FXCollections.observableArrayList(v.problem);
return problemData;
}
@Override
public ListProblem marshal(ObservableList<Problem> v) throws Exception {
ListProblem problems = new ListProblem();
v.stream().forEach((item) -> {
problems.problem.add(item);
});
return problems;
}
}
列表:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
public class ListProblem {
@XmlElement(name = "problem")
List<Problem> problem = new ArrayList<>();
public List<Problem> getproblems() {
return problem;
}
}
而且主營:
public void loadProblemDataFromFile(File file) {
try {
JAXBContext context = JAXBContext
.newInstance(ProblemListWrapper.class);
Unmarshaller um = context.createUnmarshaller();
ProblemListWrapper wrap = (ProblemListWrapper)um.unmarshal(file);
problemData.clear();
problemData.addAll(wrap.getProblems());
System.out.println(problemData.get(0).toString());
setProblemFilePath(file);
} catch (Exception e) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Could not load data");
alert.setContentText("Could not load data from file:\n" + file.getPath());
alert.showAndWait();
}
}
public void saveProblemDataToFile(File file) {
try {
JAXBContext context = JAXBContext
.newInstance(ProblemListWrapper.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
System.out.println(problemData.get(0).toString());
ProblemListWrapper wrapper = new ProblemListWrapper();
wrapper.setProblems(problemData);
m.marshal(wrapper, file);
setProblemFilePath(file);
} catch (Exception e) { // catches ANY exception
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Could not save data");
alert.setContentText("Could not save data to file:\n" + file.getPath());
alert.showAndWait();
}
}
打印時你想得到什麼?現在看起來你正在打印對象,這就是爲什麼你得到你所得到的結果 –
System.out.println(problemData.size());輸出:3,但System.out.println(problemData.get(0).toString); Outpbut:[email protected]我需要字符串,但顯示哈希碼 –
您期待從'System.out.println(...)'語句中看到什麼? –