GHads mind

developers thoughts & finds

Java to XML to Java

leave a comment »

Some simple methods using the integrated JRE-Classes (since Java 1.4) the de/serialize Objects to/from XML (must check out how to post code tonight… see below for an update):

public static Object importXML(File in) {
  try {
    FileInputStream fis = new FileInputStream(in);
    BufferedInputStream bis = new BufferedInputStream(fis);
    XMLDecoder decoder = new XMLDecoder(bis);
    Object result = decoder.readObject();
    decoder.close();
    bis.close();
    fis.close();
    return result;
  } catch (Exception e) {
    e.printStackTrace();
  }
  return null;
}
public static boolean exportXML(Object in, File out) {
  try {
    FileOutputStream fos = new FileOutputStream(out);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    XMLEncoder encoder = new XMLEncoder(bos);
    encoder.writeObject(in);
    encoder.close();
    bos.close();
    fos.close();
    return true;
  } catch (Exception e) {
    e.printStackTrace();
  }
  return false;
}

Update: Thanks to StackOverflow.com I got the answer to how to blog here in seconds! That’s really really cute!

Advertisements

Written by ghads

September 16, 2008 at 2:49 pm

Posted in Java development

Tagged with , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: