Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within I/O and Streams
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
I/O and Streams
Serialization of Java Objects to XML Problem
Greg Pata
Ranch Hand
Posts: 40
I like...
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
It seems I am not able to serialize an ArrayList<NonTrivialObject>() to xml...
Here is the debug code:
public class ZRow { private int age; public ZRow(String name, double weight, int age){ this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void printout(){ System.out.format("age %d%n", age); } }
import java.io.Serializable; import java.util.ArrayList; public class ZBean implements Serializable{ private static final long serialVersionUID = 5L; private ArrayList<ZRow> zRows = new ArrayList<ZRow>() ; public ZBean() {} public ArrayList<ZRow> getZRows() {return zRows;} public void setZRows(ArrayList<ZRow> zRows) {this.zRows = zRows;} public void printout(){ for(ZRow zrow : zRows){ zrow.printout(); } } }
import java.beans.XMLEncoder; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.util.ArrayList; public class Zgroup { private String zFileName = "zLast.xml"; private ArrayList<ZRow> rows = new ArrayList<ZRow>(); public Zgroup(){} public ArrayList<ZRow> getRows() { return rows; } public void setRows(ArrayList<ZRow> rows) { this.rows = rows; } public static void main(String[] args) { Zgroup zg = new Zgroup(); zg.test(); } public void test(){ rows.add(new ZRow("Ali", 1.0, 2)); rows.add(new ZRow("Bli", 2.0, 3)); serializeToXml(); } public void serializeToXml(){ try { ZBean zb = new ZBean(); zb.setZRows(rows); zb.printout(); FileOutputStream fos = new FileOutputStream(zFileName, false); XMLEncoder xmlEncoder = new XMLEncoder(new BufferedOutputStream(fos)); xmlEncoder.writeObject(zb); xmlEncoder.close(); } catch (Exception e) { System.out.println(e.getMessage()); } } }
The output is:
age 2 age 3 java.lang.InstantiationException: com.ac.derivatives.ZRow ...
I do not know how to correct this.
Thank you for your help.
Tony Docherty
Bartender
Posts: 3323
86
posted 7 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
The ZRow class needs to have a public no args constructor.
Greg Pata
Ranch Hand
Posts: 40
I like...
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Tony Docherty wrote:
The ZRow class needs to have a public no args constructor.
Indeed, thank you Tony.
Tony Docherty
Bartender
Posts: 3323
86
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
My pleasure.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Trying to use XMLEncoder
Serilization issue in string object ?
little problem in 2 classes' serializaiton&deserialization
How to add the objects to the arraylist class if object has many states
Problem with ObjectInputStream
More...