'Save data in an XML file

I have an application where in i need to save the data input by a user in a form in an XML file at a specified location and i need to perform this using Java . I am relatively very new to XML handling in java. I would like some suggestions as to how to start the task .

Any code snippets and links will be helpful ...

Thank You



Solution 1:[1]

There are many open source libraries, but I would simply use JAXB, the standard. Although I have to say the XStream library suggested by other answerers looks very promising, too!

Solution 2:[2]

Consider using xstream (http://x-stream.github.io/). XStream's API is very simple:

YourObjectGraph yourData=buildYourData();
XStream xstream=new XStream();
String yourXML=xstream.toXml(yourData);
// do something with your shiny XML

Importing is just as easy:

YourObjectGraph yourData=(YourObjectGraph)xstream.fromXml(yourXml);

Solution 3:[3]

I would start by looking at the XStream library. It's very simple to convert POJOs (plain old java objects) to and from XML. I have a blog post detailing some of the gotchas.

Solution 4:[4]

You can also use the java.util.Properties to save and load properties as XML file

to save xml :

storeToXML(OutputStream os, String comment);
storeToXML(OutputStream os, String comment, String encoding);

to load xml :

loadFromXML(InputStream in)

here is an example :

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class Main {

    public static void main(String[] args) {
        File file = new File(getPath());
        if (!file.exists()) {
            Properties p1 = new Properties();
            p1.setProperty("A", "Amir Ali");
            try {
                writeXML(p1);
                System.out.println("xml saved to " + getPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            try {
                Properties p2 = readXML();
                System.out.println(p2.getProperty("A"));
            } catch (InvalidPropertiesFormatException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void writeXML(Properties properties) throws IOException {
        if (properties != null) {
            OutputStream os = new FileOutputStream(getPath());
            properties.storeToXML(os, null);
        }
    }

    public static Properties readXML() throws InvalidPropertiesFormatException, IOException {
        InputStream is = new FileInputStream(getPath());
        Properties p = new Properties();
        p.loadFromXML(is);
        return p;
    }
    
    private static String getPath() {
        return System.getProperty("user.home") + File.separator + "properties.xml";
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lukas Eder
Solution 2 facundofarias
Solution 3 facundofarias
Solution 4 Amir Ali