9

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

Flash
  • 2,901
  • 5
  • 38
  • 55

5 Answers5

18

There is very good framework JAXB for this also there is Simple

But I have used this XStream

Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));

Now, to convert it to XML, all you have to do is make a simple call to XStream:

String xml = xstream.toXML(joe);

The resulting XML looks like this:

<person>
  <firstname>Joe</firstname>
  <lastname>Walnes</lastname>
  <phone>
    <code>123</code>
    <number>1234-456</number>
  </phone>
  <fax>
    <code>123</code>
    <number>9999-999</number>
  </fax>
</person>

Also See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    Seconded. This is the simplest ay I've found – GKelly Apr 07 '11 at 12:58
  • 2
    +1 - Remember that JAXB is a specification (JSR-222) with multiple implementations: Metro (the reference implementation, included in Java SE 6), EclipseLink MOXy (I'm the tech lead), Apache JaxMe, etc. Check out this XStream comparsion (http://bdoughan.blogspot.com/2010/10/how-does-jaxb-compare-to-xstream.html), and this Simple comparison (http://bdoughan.blogspot.com/2010/10/how-does-jaxb-compare-to-simple.html). – bdoughan Apr 07 '11 at 14:05
  • Please help me here: http://stackoverflow.com/questions/25230086/generate-xml-from-a-text-file-in-java –  Aug 10 '14 at 15:45
1

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.

facundofarias
  • 2,973
  • 28
  • 27
I82Much
  • 26,901
  • 13
  • 88
  • 119
  • Check out the following for more gotchas: http://stackoverflow.com/questions/906855/parse-google-geocode-with-xstream – bdoughan Apr 07 '11 at 17:16
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!

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
1

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);
facundofarias
  • 2,973
  • 28
  • 27
Joseph Ottinger
  • 4,911
  • 1
  • 22
  • 23
0

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";
    }
}
Amir Ali
  • 26
  • 1
  • 1
  • 3