Java - Change XML and then read

0

I have an application that writes in an XML and one that reads, the two work fine separately, but the problem is that when I write in XML, the other application continues reading without updating the data, as if the file had not been updated (but was).

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

NOTE: Without Try-Catch right there in the line of d = sb.build (f); immediately when I write in xml, p>

  

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);
}
    
asked by anonymous 22.05.2017 / 07:50

1 answer

0

Probably your main problem and that a read application is reading the file at the same time that the writing is writing, this can lead to the org.jdom2.input.JDOMParseException.

Try to put your code in a way that the reader does not read the file while the writer is writing, a simple suggestion is before the writer starts writing the file he deletes the original file, does the writing with the file with another one name and when it finishes putting the file with the right name, in the reader just make a check if the file exist before you start reading.

    
22.05.2017 / 12:54