0
I have an application that writes in an XML and one that reads, the two work well separately, but the problem is that when I write in XML, the other application keeps reading without updating the data, as if the file had not been updated(but it was).
Follows the code:
public int lerXml() throws JDOMException, IOException {
    File f = new File("c:/xml.xml");
    SAXBuilder sb = new SAXBuilder();
    Document d;
    try {
        d = sb.build(f);
    } catch (Exception e) {
        return 0;
    }
    Element mural = d.getRootElement();
    List elements = mural.getChildren();
    Iterator i = elements.iterator();
    while (i.hasNext()) {
        Element element = (Element) i.next();
        if (Double.parseDouble(element.getChildText("tempo")) >= mediaPlayer.getCurrentTime().toSeconds()) {
            System.out.println("Nome:" + element.getChildText("nome"));
            System.out.println("Tempo:" + element.getChildText("tempo"));
            System.out.println("Menssagem:" + element.getChildText("conteudo"));
        }
    }
    return 1;
}
OBS: Without Try-Catch on the line d = Sb.build(f); immediately when I write in xml there is an error like this:
org.jdom2.input.Jdomparseexception: Error on line 1 of Document file:/c:/xml.xml: Premature end of file.
OBS²: this function is in a Thread running non-stop.
EDIT--------------------------------------------------------------
Reading:
public int lerXml() throws JDOMException, IOException {
    File f = new File("c:/smh/xml.xml");
    if (f.exists()) {
        SAXBuilder sb = new SAXBuilder();
        Document d;
        try {
            d = sb.build(f);
        } catch (Exception e) {
            System.out.println(e);
            return 0;
        }
        Element mural = d.getRootElement();
        List elements = mural.getChildren();
        Iterator i = elements.iterator();
        while (i.hasNext()) {
            Element element = (Element) i.next();
            if (Double.parseDouble(element.getChildText("tempo")) >= mediaPlayer.getCurrentTime().toSeconds()) {
                //System.out.println("Nome:" + element.getChildText("nome"));
                //System.out.println("Tempo:" + element.getChildText("tempo"));
                System.out.println("Menssagem:" + element.getChildText("conteudo"));
            }
        }
    }
    return 1;
}
Writing:
private void xml(String name, Double currentTime, String comment) throws JDOMException, IOException {
    File f1 = new File(PATH);
    File f2 = new File(PathTemp);
    copyFile(f1, f2);
    f1.delete();
    Document myDocument = lerArquivo(PathTemp);
    Element comentarios = myDocument.getRootElement();
    Element comentario = new Element("comentario");
    comentario.setAttribute("id", "1");
    Element nome = new Element("nome");
    nome.setText(name);
    Element tempo = new Element("tempo");
    tempo.setText(currentTime.toString());
    Element conteudo = new Element("conteudo");
    conteudo.setText(comment);
    comentario.addContent(nome);
    comentario.addContent(tempo);
    comentario.addContent(conteudo);
    comentarios.addContent(comentario);
    Format format = Format.getPrettyFormat();
    format.setEncoding("ISO-8859-1");
    XMLOutputter xout = new XMLOutputter(format);
    try {
        xout.output(myDocument, System.out);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        FileWriter arquivo = new FileWriter(new File(PATH));
        xout.output(myDocument, arquivo);
    } catch (IOException e) {
        e.printStackTrace();
    }
    copyFile(f2, f1);
}
						
I’ll try that, thank you!
– Otavio Souza Rocha
I tried and it didn’t work, I’ll put the new code in the post.
– Otavio Souza Rocha