Home > Mobile >  how to add data to XML files in java
how to add data to XML files in java

Time:05-16

how can i add data to an xml file and append to it ohter data if it exists ?

i tried the following but this code only creates one node of values and does not append to the file. it always removes the existing one and add the new one to it.

import java.beans.XMLEncoder;
import java.beans.XMLDecoder;
import java.io.*;

public class Main {
    public static void main(String[] args) {
        Question quest = new Question("Mouhib 9a7boun ?", "EYY");
        try {
            FileOutputStream fos = new FileOutputStream(new File("./quest.xml"));
            XMLEncoder encoder  = new XMLEncoder(fos);
            encoder.writeObject(quest);
            encoder.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

CodePudding user response:

If you want to append addtional data to an existing file you can use FileOutputStream(File file, boolean append).

BUT this will not lead to a valid xml-file.

CodePudding user response:

You can use XML DOM editing methods: XML DOM Add Nodes

  • Related