Solved

deserialize a bean from XML

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert from a json string array to a Java object 3 76
SequenceInputStream example 3 33
how to debug htl and js pages 8 76
Website checklist for browser compatibility? 2 52
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

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