开发者

Save data in an XML file

开发者 https://www.devze.com 2023-02-22 14:31 出处:网络
I have an application开发者_Python百科 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 ne

I have an application开发者_Python百科 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


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

  • JAXB
  • where-i-can-find-a-detailed-comparison-of-java-xml-frameworks


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!


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


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.


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";
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消