2017-10-07 34 views
-1

Je souhaite enregistrer et charger des questions à partir d'une enquête à partir d'une application JavaFx. Les méthodes pour enregistrer la classe dans le fichier .xml avec JAXB fonctionnent correctement, mais apparemment je ne peux pas faire fonctionner le unmarshaller. Chaque fois que je sélectionne un fichier .xml à charger, rien ne se passe. Pas d'erreur. Les valeurs de la question différente ne changent pas pour les sauvegardés.JAXB: XML unmarshaller ne charge rien

J'ai suivi le guide this étape par étape, mais je ne trouve pas la pièce manquante.

Méthodes de charge:

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); 
    } 

} 

Question Classe:

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); 
} 
} 

classe Sondage:

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> 

Est-ce que quelqu'un sait ce qui manque ou ce qui peut se tromper? Merci d'avance!

EDIT: Je changé le nom de getQuestions() de QuestionListWrapper, parce que je pensais que peut-être il ne se charge pas correctement parce que les méthodes de Survey et QuestionListWrapper ont le même nom. Puis-je obtenir l'erreur suivante:

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) 
+0

"Rien ne se passe"? Eh bien, apparemment quelque chose se passe. Avez-vous des erreurs? À quoi ressemblent votre XML et 'QuestionListWrapper'? – lexicore

+0

@lexicore Mise à jour de la question. Je ne reçois aucune erreur. Les valeurs des questions dans 'survey' ne changent pas lorsque je charge le fichier .xml dans l'application. – jeypeu

+0

@lexicore J'ai trouvé une source d'erreur possible (voir EDIT). – jeypeu

Répondre

0

Ajout @XmlElementWrapper avant @XmlElement(name = "question") devrait fonctionner.