JAXB:XML unmarshaller没有加载任何东西

时间:2017-10-07 15:41:46

标签: java xml javafx jaxb javafx-8

我想从JavaFx应用程序中保存和加载调查中的问题。使用JAXB将类保存到.xml文件的方法正常工作,但显然我无法让unmarshaller工作。每次我选择加载.xml时,都没有任何反应。没错。不同问题的值不会更改为已保存的值。

我一步一步地跟着this向导,但我找不到丢失的部分。

加载方法:

public void loadQuestions(File file) throws Exception{

    JAXBContext context = JAXBContext.newInstance(QuestionListWrapper.class);
    Unmarshaller um = context.createUnmarshaller();

    QuestionListWrapper wrapper = (QuestionListWrapper) um.unmarshal(file);

    survey.getQuestions().clear();
    survey.getQuestions().addAll(wrapper.getQuestions());

}

@FXML private Button bLoad;

@FXML
private void handleLoad() throws Exception{

    Stage stage = (Stage) bLoad.getScene().getWindow();

    FileChooser fileChooser = new FileChooser();
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("XML files (*.xml)", "*.xml");
    fileChooser.getExtensionFilters().add(extFilter);

    File file = fileChooser.showOpenDialog(stage);

    if (file != null){
      loadQuestions(file);
    }

}

问题类:

package survey.model;

import javafx.beans.property.*;

public class Question {

private StringProperty text;
private BooleanProperty answer;
private StringProperty comment;
private StringProperty information;

public Question() {
    text = new SimpleStringProperty("");
    answer = new SimpleBooleanProperty(false);
    comment = new SimpleStringProperty("");
    information = new SimpleStringProperty("");

}

public String getInformation() {
    return information.get();
}

public StringProperty informationProperty() {
    return information;
}

public void setInformation(String information) {
    this.information.set(information);
}

public String getComment() { return comment.get(); }

public StringProperty commentProperty() {
    return comment;
}

public void setComment(String comment) {
    this.comment.set(comment);
}


public Question(String text) {
    setText(text);
}


public boolean isAnswer() {
    return answer.get();
}

public BooleanProperty answerProperty() {
    return answer;
}

public void setAnswer(boolean answer) {
    this.answer.set(answer);
}


public String getText() {
    return text.get();
}

public StringProperty textProperty() {
    return text;
}

public void setText(String text) {
    this.text.set(text);
}
}

调查班:

package survey.model;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class Survey {

private ObservableList<Question> questions = FXCollections.observableArrayList();

public ObservableList<Question> getQuestions() {
    return questions;
}

}

QuestionListWrapper:。

package survey.model;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;

@XmlRootElement(name = "questions")
public class QuestionListWrapper {

private List<Question> questions;

@XmlElement(name = "question")
public List<Question> getQuestions(){
    return questions;
}

public void setQuestions(List<Question> questions){
    this.questions = questions;
}

}

XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<questions>
<question>
    <answer>true</answer>
    <comment></comment>
    <information>asfd</information>
    <text>Der Stelleninhaber wird in verschiedenen Tätigkeitsbereichen eingesetzt (z.B. durch ein vorhandenes Rotationssystem).</text>
</question>
<question>
    <answer>true</answer>
    <comment></comment>
    <information>sdasd</information>
    <text>Die Zuteilung und Erweiterung von Tätigkeitsbereichen erfolgt unter Berücksichtigung der Fähigkeiten, Kenntnisse und Wünsche des Stelleninhabers.</text>
</question>
<question>
    <answer>false</answer>
    <comment>asdasd</comment>
    <information>sadsad</information>
    <text>Gleiche Teiltätigkeiten wiederholen sich so häufig, dass die Arbeit monoton und eintönig erscheint.</text>
</question>
<question>
    <answer>false</answer>
    <comment></comment>
    <information>asdsad</information>
    <text>Die Arbeit wird hauptsächlich in einer Körperhaltung ausgeführt</text>
</question>
</questions>

有谁知道遗失的内容或可能出错的内容?提前谢谢!

编辑: 我从getQuestions()更改了QuestionListWrapper的名称,因为我认为它可能无法正确加载,因为SurveyQuestionListWrapper的方法具有相同的名称。然后我收到以下错误:

java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:289)
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:253)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Scope.add(Scope.java:106)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty$ReceiverImpl.receive(ArrayERProperty.java:198)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(UnmarshallingContext.java:597)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:165)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2967)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189)
at survey.view.moduleAabController.loadQuestions(moduleAabController.java:306)
at survey.view.moduleAabController.handleLoad(moduleAabController.java:325)
at survey.view.moduleAabController.handleButtonAction(moduleAabController.java:258)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745) 

1 个答案:

答案 0 :(得分:0)

@XmlElementWrapper之前添加@XmlElement(name = "question")应该有效。