RSS

Read XML file to Object in Java

07 Sep

Read this first persist-java-object-to-xml

Here I’m doing just read the data from given XML file

XmlToBean.java

package mysamples;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;

/**
 *
 * @author dinuka
 */
public class XmlToBean {

public static void main(String[] args) {
 try {

//Read the xml file and set data to MyBean Object
 MyBean myReadBean = new MyBean();
 XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream("MyBean.xml")), myReadBean);
 myReadBean = (MyBean) decoder.readObject();
 decoder.close();

 //print the myReadBean object
 myReadBean.print();

} catch (Exception e) {
 e.printStackTrace();
 }
 }
}

Advertisements
 
1 Comment

Posted by on September 7, 2012 in java

 

Tags: , ,

One response to “Read XML file to Object in Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: