Java - Change XML and then read

Asked

Viewed 345 times

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

1 answer

-1

Probably your main problem is that a reading application is reading the file at the same time as the writing one is writing, it may result in org.jdom2.input.Jdomparseexception.

Try to put your code in a way that the reader doesn’t read the file while the writer is writing, a simple suggestion is before the writer starts writing the file he erases the original file, writes to the file with another name and when it is finished put the file with the right name, in the reader just make a check if the file exists before you start reading.

  • I’ll try that, thank you!

  • I tried and it didn’t work, I’ll put the new code in the post.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.