Showing posts with label xml to object in java. Show all posts
Showing posts with label xml to object in java. Show all posts

Friday, September 7, 2012

Read XML file to Object in Java

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

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

XmlToBean.java

[sourcecode language="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();
}
}
}

[/sourcecode]