1

I've been reading over this SAXParseException error for a while and trying to resolve it but to no success.

[Fatal Error] schedule.xml:1:1: Premature end of file. 
org.xml.sax.SAXParseException; systemId: file:/U:/schedule.xml; lineNumber: 1; columnNumber: 1; Premature end of file.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
    at package.DOMclass.importDocument(DOMclass.java:681) //<---.parse(new File(fileToImport));
    ...

I've read how it could be the stream being used is empty (or xml file being read is empty), as well as it can't be reused? Also, the problem of reading and writing back to the same file. Here's a link that explains it but I still don't understand.

Also, another possibility could be that my xml is malformed/not correct but I've looked over it and it's fine, the tags are good and I've encoded it in UTF-8 (without BOM) in Notepad++ since some posts have said there might be hidden whitespace before the xml declaration but again, no success.

This is my reading in code

public TestClass(){
   Node root = DOMclass.importDocument("U:\\schedule.xml");
   ... read nodes, attribute values, etc..
}

public static Document importDocument(String fileToImport)
   {
      Document document = null;
      try
      {
         document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new File(fileToImport));  //<--- where the error is at 
         //Do I need a Reader here? to close?

      }
      catch(SAXException saxe)
      {
         saxe.printStackTrace();
      }
      catch(IOException ioe)
      {
         ioe.printStackTrace();
      }
      catch(ParserConfigurationException pce)
      {
         pce.printStackTrace();
      }  
      return document;
   }

This is my writing out.

public static void addNode(String name, String lastName,...){
    String filepath = "U;\\schedule.xml";
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder;

    try {
            docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(filepath);
...
... make/traverse xml nodes, elements, appending
...
}
DOMUtils.exportDocument(doc, filepath);

    } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }

public static void exportDocument(Document documentToExport, String fileName)
   {
      Transformer transformer = null;
      DOMSource domSource = null;
      StreamResult streamResult = null;
      try
      {
         BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
         transformer = TransformerFactory.newInstance().newTransformer();
         domSource = new DOMSource(documentToExport);
         streamResult = new StreamResult(out);

         System.out.println("\nStream Result: " + streamResult);

         transformer.transform(domSource, streamResult); 
         out.close();
      }
      catch (IOException e)
      {
         e.printStackTrace();
      } 
      catch(TransformerException e)
      {
         e.printStackTrace();
      }       
      finally
      {
         transformer = null;
         domSource = null;
         streamResult = null;         
      }
   } 
}

Any thoughts? Thank you.

E.C
  • 45
  • 1
  • 7
  • Have you tried not writing to the same file you are reading? This pattern is a terrible idea for many reasons and will cause issues if it does anything other than load the entire file into memory at the beginning. Premature end of the file suggest a malformed file. – Deadron Nov 01 '18 at 17:48
  • String filepath = "U;\\schedule.xml"; Is that supposed to be a :? – Justin Nov 01 '18 at 18:04
  • @Justin Thanks for pointing it out, I just typed it in wrong but on my code, it's written right. Edit: wording – E.C Nov 01 '18 at 18:25
  • @Deadron I read that but how will I be able to get the most recent data? It's a javaFX TableView application that when the file is modified, the TableView will refresh i.e., read the file that was just written to by the other running application and set the new data. – E.C Nov 01 '18 at 18:25
  • @E.C Write to a temporary file and then overwrite the old file with the new file with a file system copy/move. However, you have a new problem with those details you just shared. You will not be able to overwrite a file that is opened and being read by another program. You might be able to get away with retrying until its not reading it but that assumes the program releases the file. Ideally you should lock the file first to prevent concurrency issues but this may cause issues with your main app. – Deadron Nov 01 '18 at 18:34
  • @Deadron Oh okay, but can I immediately close my old file after reading? would that help? I'm probably wrong in interpreting your recommendation when you say lock, because to me that sounds like I would lock the file to not be read and written too. But there might be tens of these running applications at once. – E.C Nov 01 '18 at 18:45
  • @E.C Files are not designed for concurrent access and modification. This is probably the actual issue here. There is no good solution to this other than migrating to a solution designed for concurrent access eg. a database – Deadron Nov 01 '18 at 18:49
  • @Deadron ahh ok, hopefully it's not too bad to switch over. Thanks for your help. I'll probably come back and post another comment as to what I ended up doing. – E.C Nov 01 '18 at 19:10
  • @Deadron so for now, I have a temp file and it seems to be working ok so far. The tableview sometimes do not immediately refresh on other running applications (using WatchService) for every action but when it does update, it updates to the most recent data. If I do end up having problems though, I'll know to use a database then. Thank you again for helping me out. Good fortune to you. – E.C Nov 01 '18 at 20:02

0 Answers0