无法读取最新版本的XML文件

时间:2018-03-14 10:24:44

标签: java xml stax

我创建了一个应用程序,它从文本字段中获取用户输入并将其存储在XML文件中。然后,用户可以通过单击按钮将这些输入重新加载到文本字段中。

我遇到的问题是没有使用最新版本的XML文件 - 例如,当我启动应用程序并保存新输入时,加载功能无法加载输入,即使XML文件中包含数据。当我关闭然后重新启动应用程序时,加载功能使用上一个会话中的数据。如果我尝试添加新输入,则加载按钮将仅循环上一个会话中的数据,而不是使用新点。

所以我的问题是为什么不读取XML文件的当前状态?

保存到XML代码:

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

加载按钮代码:

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

        }
    });

0 个答案:

没有答案