-1

So I'm making a calendar program and I need it to update when you add a new entry to it. Right now, I need to click on the xml file to get it to update, then everything else works fine.

Declaration:

    private DocumentBuilderFactory documentFactory;
    private DocumentBuilder documentBuilder;
    private Document xmlDoc;
    private Node rootNode;
    private static Node dataNode;

Assignment in constructor:

    try {
        documentFactory = DocumentBuilderFactory.newInstance();
        documentBuilder = documentFactory.newDocumentBuilder();
        xmlDoc = documentBuilder.parse(Main.class.getResourceAsStream("Calendar.xml"));
        rootNode = xmlDoc.getDocumentElement();
        dataNode = rootNode.getChildNodes().item(0);
    } catch(ParserConfigurationException | SAXException | IOException e) {e.printStackTrace(System.out);}

Node is created and added to dataNode after a button is pressed, then the file is updated like this:

    try {
        OutputFormat outFormat = new OutputFormat(xmlDoc);

        try (FileOutputStream outStream = new FileOutputStream("src/virtualagenda/Calendar.xml")) {
            XMLSerializer serializer = new XMLSerializer(outStream, outFormat);
            serializer.serialize(xmlDoc);

            outStream.flush();
            outStream.close();
        }
    }catch(IOException e) {e.printStackTrace(System.out);}
alexanderd5398
  • 332
  • 1
  • 4
  • 16
  • 1
    What is your problem/error? – henje Jan 18 '15 at 22:25
  • @henje That the XML file is not updating as soon as I save it. I need to click on the XML file outside of the program to get it to update. – alexanderd5398 Jan 18 '15 at 22:31
  • That's not correct. The code you've posted writes to the file immediately. Clicking on a file outside the program can't cause the program to write the file. The suggestion doesn't begin to make sense. To avoid having to 'click on the XML file ... to get it to update' I suggest you try F5 or whatever refreshes your file explorer. This is not a coding problem. NB flush before close is redundant. – user207421 Jan 19 '15 at 00:51
  • @EJP Well before I click on the file, the program doesn't display whatever I wrote to it. After I click on it, the program displays the new node correctly. – alexanderd5398 Jan 19 '15 at 01:27
  • And "I suggest you try F5 or whatever refreshes your file explorer" doesn't help my problem at all. – alexanderd5398 Jan 19 '15 at 01:34
  • Not stating your problem in an intelligible form doesn't help you get solutions here. I've done my best but enough guessing is enough. – user207421 Jan 20 '15 at 04:38
  • @EJP I've tried to show my problem as best as I can. I'm obviously a beginner so instead you could suggest where else this problem might be occurring. And I don't appreciate you marking my other questions as duplicates. I've added more details there and tried something different. I reposted it so it so more people would see it. I'm trying to solve my problem and you're not helping. – alexanderd5398 Jan 20 '15 at 15:11

1 Answers1

0

Rather than loading your document in the constructor you should create some sub processes, such as

  1. Load the XML from the file into a Document
  2. Create/Update your GUI, given a Document parameter
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80