Java - Add information in XML file with Jdom

Asked

Viewed 274 times

1

In this code an xml file is created and an element is inserted:

comentarios = new Element("comentarios");
    myDocument = new Document(comentarios);
    comentario = new Element("comentario");
    comentario.setAttribute("id", "123");

    Element nome = new Element("nome");
    nome.setText("Otavio");
    Element tempo = new Element("tempo");
    tempo.setText("0.00");
    Element conteudo = new Element("conteudo");
    conteudo.setText("Teste de Comentário");

    comentario.addContent(nome);
    comentario.addContent(tempo);
    comentario.addContent(conteudo);

    comentarios.addContent(comentario);

    XMLOutputter xout = new XMLOutputter();
    try {
        xout.output(myDocument, System.out);
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        FileWriter arquivo = new FileWriter(new File("c:/xml.xml"));
        xout.output(myDocument, arquivo);
    } catch (IOException e) {
        e.printStackTrace();
    }

Current code overwrites the original contents of the file, creating a new file.

How to run the file 10 times, the file has to contain 10 data in it, adding a new element to the end of the file?

1 answer

1


First you have to read the file before adding the new comments:

private static Document lerArquivo(String path) throws FileNotFoundException, JDOMException, IOException{
    Document doc = null;
    Element raiz = null;
    File xmlFile = new File(path);
if(xmlFile.exists()) {
    //ler arquivo se encontrado
    FileInputStream fis = new FileInputStream(xmlFile);
    SAXBuilder sb = new SAXBuilder();
    doc = sb.build(fis);
    fis.close();
} else {
    //cria arquivo se não existir
    doc = new Document();
    raiz = new Element("comentarios");
    }
return doc;
}

Then you have to use the root element to add new comments:

final String PATH = "C:\\xml.xml";
Document myDocument = lerArquivo(PATH);
Element comentarios = myDocument.getRootElement();
Element comentario = new Element("comentario");

The complete example:

import java.io.*;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;

public class JDOMExemplo{
    final String PATH="C:\\xml.xml";
    public static void main(String[] args) throws Exception {

    Document myDocument = lerArquivo(PATH);
    Element comentarios = myDocument.getRootElement();
    Element comentario = new Element("comentario");
    comentario.setAttribute("id", "123");

    Element nome = new Element("nome");
    nome.setText("Otavio");
    Element tempo = new Element("tempo");
    tempo.setText("0.00");
    Element conteudo = new Element("conteudo");
    conteudo.setText("Teste de Comentário");

    comentario.addContent(nome);
    comentario.addContent(tempo);
    comentario.addContent(conteudo);
    comentarios.addContent(comentario);


    XMLOutputter xout = new XMLOutputter();
    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();
    }
    }

    private static Document lerArquivo(String path) throws FileNotFoundException, JDOMException, IOException{
        Document doc = null;
        Element raiz = null;
        File xmlFile = new File(path);
    if(xmlFile.exists()) {
        // try to load document from xml file if it exist
        // create a file input stream
        FileInputStream fis = new FileInputStream(xmlFile);
        SAXBuilder sb = new SAXBuilder();
        doc = sb.build(fis);
        raiz = doc.getRootElement();
        fis.close();
    } else {
        // if it does not exist create a new document and new root
        doc = new Document();
        raiz = new Element("comentarios");
        }
    return doc;
    }

}

Browser other questions tagged

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