简体   繁体   中英

how to add data to XML files in java

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

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.

您可以使用 XML DOM 编辑方法: XML DOM Add Nodes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM