Solved

deserialize a bean from XML

Posted on 2013-11-26
4
445 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
noX challenge 17 75
nestparen challenge 4 55
create a gui in perl 3 42
Checkbox and ListView in Android Layout 4 35
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.

762 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

6 Experts available now in Live!

Get 1:1 Help Now