Solved

deserialize a bean from XML

Posted on 2013-11-26
4
453 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now