Solved

deserialize a bean from XML

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 58
how to install java on RHEL image on EC2 4 40
by zero exception 10 52
Cisco ASA: Java web start no go, asdm launcher no go 3 34
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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 video teaches viewers about errors in exception handling.

840 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