CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2005
    Posts
    33

    Question Write/Read Array of BeanObjects using XMLEncoder

    Hi,

    This is very urgent. I've designed a feedback Bean.

    import java.io.Serializable ;

    public class FeedbackBean implements Serializable{

    private String assetName;
    private String comments;
    private String reusability;
    private String userName;
    private String date;

    public FeedbackBean() {

    }

    /**
    * @return String
    */
    public String getAssetName() {
    return assetName;
    }

    /**
    * @param dt string
    */
    public void setAssetName(String an) {
    assetName = an;
    }

    /**
    * @return String
    */
    public String getComments() {
    return comments;
    }

    /**
    * @param comment string
    */
    public void setComments(String com) {
    comments = com;
    }

    /**
    * @return String
    */
    public String getReusability() {
    return reusability;
    }

    /**
    * @param reuse String
    */
    public void setReusability(String reuse) {
    reusability = reuse;
    }

    /**
    * @return String
    */
    public String getDate() {
    return date;
    }

    /**
    * @param dt string
    */
    public void setDate(String dt) {
    date = dt;
    }


    /**
    * @return String
    */
    public String getUserName() {
    return userName;
    }

    /**
    * @param name string
    */
    public void setUserName(String name) {
    userName = name;
    }
    }


    I added these feedback bean objects to the FeedbackCollection object

    import java.io.Serializable;
    import java.util.Iterator;
    import java.util.*;


    public class FeedbackCollection implements Serializable {

    private List feedbackList = null;

    public FeedbackCollection() {
    feedbackList = new ArrayList();
    }

    public void addFeedback(FeedbackBean fb) {
    feedbackList.add(fb);
    }


    public ListIterator getFeedback() {
    return feedbackList.listIterator();
    }

    public FeedbackBean getFeedback(int index) {
    return (FeedbackBean)feedbackList.get(index);
    }
    }


    If I write this Feedback Collection Object to xml, will I be able to read/write the individual Feedback Bean objects?

    I've attached the piece of code which I tried


    import java.io.Serializable ;
    import java.io.*;
    import java.beans.*;
    import java.util.*;

    public class FileWriter {

    public FileWriter() {}

    public void writeTo(String fileName, FeedbackCollection fbc) throws Exception {
    if (fileName == null || fileName.equals("")) {
    throw new FileNotFoundException("Invalid File Name");
    }

    File f = new File(fileName);
    if (f.exists()) {

    FileInputStream in = new FileInputStream(fileName);
    ObjectInputStream s = new ObjectInputStream(in);
    //FeedbackCollection decode = (FeedbackCollection) s.readObject();

    //ListIterator it = decode.getFeedback();

    while (it.hasNext()) {
    // Get element
    FeedbackBean temp = (FeedbackBean) it.next();
    System.out.println(temp.getAssetName());
    System.out.println(temp.getComments());
    System.out.println(temp.getReusability());
    System.out.println(temp.getUserName());
    System.out.println(temp.getDate());
    }
    }
    else
    {
    FileOutputStream out = new FileOutputStream(fileName);
    ObjectOutputStream s = new ObjectOutputStream(out);
    s.writeObject(fbc);
    s.flush();
    }
    }

    public static void main(String args[]) throws Exception {
    FeedbackBean fb1 = new FeedbackBean();
    fb1.setAssetName("MyAsset");
    fb1.setComments("Excellent");
    fb1.setReusability("Excellent");
    fb1.setUserName("serName");
    fb1.setDate("date");

    /*FeedbackBean fb2 = new FeedbackBean();
    fb2.setAssetName("Asset2");
    fb2.setComments("Excell");
    fb2.setReusability("Exc");
    fb2.setUserName("ser");
    fb2.setDate("today");

    FeedbackCollection collection = new FeedbackCollection();
    collection.addFeedback(fb1);
    collection.addFeedback(fb2);*/

    FileWriter writer = new FileWriter();
    writer.writeTo("temp.obj", fb1);
    }

    }

    This is really urgent..

    Any body there to help me out...

  2. #2
    Join Date
    Jun 2005
    Posts
    33

    Re: Write/Read Array of BeanObjects using XMLEncoder

    I solved the reading/writing part to the xml.

    But is there a way to make the xml file - well formatted.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured