Solved

deserialize a bean from XML

Posted on 2013-11-26
4
509 Views
Last Modified: 2013-12-03
Hi,

I was going through below link

http://www.java-tips.org/java-se-tips/java.beans/how-to-deserialize-a-bean-from-xml.html

I have not understood what author meant by

This Java tip illustrates a method of deserialize a bean from XML. For developer information, when a Bean instance is serialized, it is converted into a data stream and written to storage. Any applet, application, or tool that uses that Bean can then "reconstitute" it by deserialization.
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 14

Expert Comment

by:jb1dev
ID: 39679483
Any instance of a java class which is "bean compliant" (http://en.wikipedia.org/wiki/JavaBeans
They are serializable, have a 0-argument constructor, and allow access to properties using getter and setter methods.) can be persisted into XML and loaded back into in memory object instances using

XMLDecoder
http://docs.oracle.com/javase/7/docs/api//java/beans/XMLDecoder.html

XMLEncoder
http://docs.oracle.com/javase/7/docs/api/java/beans/XMLEncoder.html

E.g.

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

public class SerializeMe {

    private String _name;
    private Date _date;
    private int _id;

    //
    // Need a default constructor to be bean compliant
    //
    public SerializeMe() { }

    public SerializeMe( String name, 
                        Date date,
                        int id ) {
        _name = name;
        _date = date;
        _id = id;
    }

    //
    // Need getters and setters for all of our attributes to be
    // bean compliant.
    //
    public String getName() { return _name; }
    public Date getDate() { return _date; }
    public int getId() { return _id; }

    public void setName(String s) { _name = s; }
    public void setDate(Date d) { _date = d; }
    public void setId(int i) { _id = i; }

    public String toString() {
        return "[ Name: " + _name + " Date: " + _date + " ID: " + _id + " ]";
    }

    
    public static void main(String[] args) throws Exception {
        SerializeMe foo = new SerializeMe("Joe", new Date(), 1001);
        System.out.println("Created object " + foo);

        // Persist object
        XMLEncoder e = new XMLEncoder(
                new BufferedOutputStream(
                        new FileOutputStream("Test.xml")));
        e.writeObject(foo);
        e.close();

        //
        // ...
        //

        //
        // Load the object instance back from xml
        //
        XMLDecoder d = new XMLDecoder(
                          new BufferedInputStream(
                              new FileInputStream("Test.xml")));
        SerializeMe baz = (SerializeMe)d.readObject();
        d.close();

        System.out.println("We loaded " + baz);
    }
}

Open in new window

0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39679503
You should check the javadocs for the classes used in the tutorial you linked to.

Also, from the XMLEncoder javadoc


The XMLEncoder class is a complementary alternative to the ObjectOutputStream and can used to generate a textual representation of a JavaBean in the same way that the ObjectOutputStream can be used to create binary representation of Serializable objects.

If you use firefox, here's a nice extension that adds a javadoc lookup to your context menu
https://addons.mozilla.org/en-US/firefox/addon/javadoc-lookup-extension/?src=ss

So you can browse straight to the javadoc from your browser's context menu for any selected/highlighted class name.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39681289
So you can browse straight to the javadoc from your browser's context menu for any selected/highlighted class name. 

Open in new window


I have installed this extension. How to use it.

I opened link

http://www.java-tips.org/java-se-tips/java.beans/how-to-deserialize-a-bean-from-xml.html


I  right clicked on the 'XMLDecoder ' to see its java doc but could not see. I am not sure how to 'browse straight to the javadoc from your browser's context menu for any selected/highlighted class name' . please advise
0
 
LVL 14

Accepted Solution

by:
jb1dev earned 500 total points
ID: 39681865
When you highlight/select a class name in a web page with the mouse cursor, and right click, you should see a "javadoc" option menu, which should have the selected class as a submenu.

E.g. in this screenshot, user has selected the "ArrayUtils" class name:

 https://addons.cdn.mozilla.net/img/uploads/previews/full/90/90184.png?modified=1361006315

When you click on the class name in the context menu, it will open a new tab to the javadoc for that class.

Though we are really straying from the original question here.
I just posted that as a tool which might help you if you are new to java.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question