XMLEncoder in Java for serialization

I'm just wondering how I use XMLEncoderfor serialization ArrayList<foo>, where foo is my own class.

Do I need to do something, in particular, first define my own xml structure, and then call toString for each value in my list and write it?

Can someone point me to a good tutorial? http://java.sun.com/products/jfc/tsc/articles/persistence4/ What I watched but does not seem to mention what to do with non-library classes.

thank

+5
source share
4 answers

If you are looking for XML serialization, I suggest you upgrade to XStream

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

String xml = xstream.toXML(joe);

<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>
+9

ArrayList XMLEncoder.

:

bean TestBean:

public class TestBean {

  private String name;
  private int age;

  public TestBean() {
    this.name = "";
    this.age = 0;
  }

  public TestBean(String name, int age) {
    this.name = name;
    this.age = age;
  }

  // Getter and setter ...

  @Override
  public String toString() {
    return String.format("[TestBean: name='%s', age=%d]", name, age);
  }
}

Main, ArrayList<TestBean> :

public class Main {
  private static final String FILENAME = "testbeanlist.xml";

  public static void main(String[] args) {
    try {
      // Create a list of TestBean objects ...
      final List<TestBean> list = new ArrayList<TestBean>();
      list.add(new TestBean("Henry", 42));
      list.add(new TestBean("Tom", 11));

      System.out.println("Writing list to file " + FILENAME + ": " + list);

      // ... and serialize it via XMLEncoder to file testbeanlist.xml
      final XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
          new FileOutputStream(FILENAME)));
      encoder.writeObject(list);
      encoder.close();

      // Use XMLDecoder to read the same XML file in.
      final XMLDecoder decoder = new XMLDecoder(new FileInputStream(FILENAME));
      final List<TestBean> listFromFile = (List<TestBean>) decoder.readObject();
      decoder.close();

      System.out.println("Reading list: " + listFromFile);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }
}

:

Writing list to file testbeanlist.xml: [[TestBean: name='Henry', age=42], [TestBean: name='Tom', age=11]]
Reading list: [[TestBean: name='Henry', age=42], [TestBean: name='Tom', age=11]]

toString() TestBean . XML.

+10

XMLEncoder , . , , , , , .

JavaBeans, encoder.writeObject() List<Foo>. , XML - , . PersistenceDelegate , , . , ( ), - - , , JavaBean.

, , .. , () , . DefaultPersistenceDelegate, , - , :

PersistenceDelegate fooDelegate = new DefaultPersistenceDelegate(new String[] {"propertyName1", "propertyName2"});
encoder.setPersistenceDelegate(Foo.class, fooDelegate);

, getter, / , , , , PersistenceDelegate . , , , , , , - , .

+2

I am not sure why so many examples on the Internet are incomplete or do not work, when they will receive little attention, so that they are much more useful. Here is a complete example that can be implemented in a single file (PersonBean.java) and it works!

// This example creates two PersonBeans, creates an ArrayList, adds the beans to the
// list, serializes the ArrayList to an XML file.
// It then loads from the XML file into a new ArrayList
//
// Keywords: ArrayList, Serialize, XMLEncode, XMLDecode
// Note: Change the XML file while the 10 second Thread.sleep is waiting to see that
// the data is actually loaded from the file.

import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.beans.XMLEncoder;
import java.beans.XMLDecoder;
import java.util.ArrayList;

public class PersonBean {
  private String name;
  private int    age;

  public String getName() {
    return name; 
  }

  public int getAge() {  
    return age;
  }

  public void setName(String name) { 
    this.name = name; 
  }

  public void setAge(int age) { 
    this.age = age; 
  }

  @Override
  public String toString() {
    return String.format("[PersonBean: name='%s', age=%d]", name, age);
  }

  public static void main(String[] args) {
    PersonBean person1 = new PersonBean();

    person1.setName("Joe");
    person1.setAge(30);

    PersonBean person2 = new PersonBean();

    person2.setName("Jane");
    person2.setAge(25);

    ArrayList arrayList1 = new ArrayList();
    arrayList1.add(person1);
    arrayList1.add(person2);

    try {
      System.out.println("List 'arrayList1' = '" + arrayList1 + "'");
      FileOutputStream outputStream = new FileOutputStream("PersonBean.xml");
      XMLEncoder encoder = new XMLEncoder(outputStream);
      encoder.writeObject(arrayList1);
      encoder.close();
      Thread.sleep(10000);
    } catch (Exception ex) {
    } 

    try {
      FileInputStream inputStream = new FileInputStream("PersonBean.xml");
      XMLDecoder decoder = new XMLDecoder(inputStream);
      ArrayList<PersonBean> arrayList2 = (ArrayList<PersonBean>) decoder.readObject();
      decoder.close();
      System.out.println("List 'arrayList2' = '" + arrayList2 + "'");
    } catch (Exception ex) {
    } 
  }
}
0
source

All Articles