Most recent version of XML file not being read

McRoland Source

I have created an application which takes user input from textfields and stores it in an XML file. The user can then reload these inputs back into a text field on the click of a button.

The issue I'm having is that the most recent version of the XML file is not being used - for example when I start the application and save new inputs the load function doesn't load the input even though the XML file has the data in it. When I close and then restart the application the load functionality works using the data from the last session. If I try adding new inputs the load button will just cycle through the data from the last session, not using the new points.

So my question is why isn't the current state of the XML file being read?

Save to XML Code:

    ArrayList<SavedPosition> posList = new ArrayList<SavedPosition>();
    String SAVEDPOS_XML = "savedPos.xml";        
    save.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) { 

        SavedPosition saved = new SavedPosition();
        saved.setPosition1(mediaPlayerComponent.getMediaPlayer().getTime());
        saved.setPosition2(mediaPlayerComponent2.getMediaPlayer().getTime());
        saved.setNBehaviour(textArea.getText());
        saved.setABehaviour(textArea1.getText());
        saved.setGap(textArea2.getText());
        saved.setForces(textArea3.getText());
        saved.setFindings(textArea2.getText());
        posList.add(saved);

        SavedPositions savedPos = new SavedPositions();
        savedPos.setSavedPositions(posList);

            try {
                // create JAXB context and instantiate marshaller
                JAXBContext context = JAXBContext.newInstance(SavedPositions.class);
                Marshaller m = context.createMarshaller();
                m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

                //print
                m.marshal(savedPos, System.out);

                //write to file
                m.marshal(savedPos, new File(SAVEDPOS_XML));

            } catch (JAXBException ex) {
                Logger.getLogger(DoubleViewer.class.getName()).log(Level.SEVERE, null, ex);
            }


        }
    });

XML to Object code:

public class XmlToObject {

public static ArrayList<ArrayList<String>> main() {

ArrayList<ArrayList<String>> obj1 = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> obj = new ArrayList<ArrayList<String>>();

try{
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document d = db.parse("savedPos.xml");
    XPath xp = XPathFactory.newInstance().newXPath();
    NodeList nl = (NodeList) xp.compile("//savedPosition").evaluate(d, XPathConstants.NODESET);
    System.out.println("number of saved positions " + nl.getLength());

    for (int i = 0; i < nl.getLength(); i++){

        String a = (xp.compile("./NBehaviour").evaluate(nl.item(i)));
        String b = (xp.compile("./ABehaviour").evaluate(nl.item(i)));
        String c = (xp.compile("./gap").evaluate(nl.item(i)));
        String d1 = (xp.compile("./forces").evaluate(nl.item(i)));
        String e = (xp.compile("./findings").evaluate(nl.item(i)));
        String f = (xp.compile("./position1").evaluate(nl.item(i)));
        String g = (xp.compile("./position2").evaluate(nl.item(i)));

        ArrayList<String> savedPosition = new ArrayList<String>();

        savedPosition.add(a);
        savedPosition.add(b);
        savedPosition.add(c);
        savedPosition.add(d1);
        savedPosition.add(e);
        savedPosition.add(f);
        savedPosition.add(g);

        obj.add(savedPosition);

    }

    return obj;

    }catch(Exception l){
        System.out.println(l.getMessage());
    }

    return obj1;
    }
}

Load button code:

    List<ArrayList<String>> obj = XmlToObject.main();
    int a = obj.size();

    load.addActionListener(new ActionListener() {

        int displayedPositionIndex = 0;

        public void actionPerformed(ActionEvent e) {

            displayedPositionIndex++;
            if(displayedPositionIndex >= obj.size()) {
                displayedPositionIndex = 0; // to loop back to first after last position
            }

            setPosition(displayedPositionIndex);
        }

        private void setPosition(int index) {  

            List<String> positionData = obj.get(index);
            textArea.setText(positionData.get(0));
            textArea1.setText(positionData.get(1));
            textArea2.setText(positionData.get(2));
            textArea3.setText(positionData.get(3));
            textArea4.setText(positionData.get(4));
            Long position = Long.valueOf(positionData.get(5));
            mediaPlayerComponent.getMediaPlayer().setTime(position);
            mediaPlayerComponent2.getMediaPlayer().setPosition(Float.parseFloat(positionData.get(6)));

        }
    });
javaxmlstax

Answers

comments powered by Disqus