|
How do I read a JavaBean from an XML file using JOX?
Author: Deron Eriksson
Description: This Java tutorial describes how to read a JavaBean from an XML file using JOX (Java Objects in XML).
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1
In another tutorial, we saw how we could use JOX (Java Objects in XMLW) to write a JavaBeanW to an XML file. JOX can be obtained from http://www.wutka.com/jox.html. In this lesson, we'll show how JOX can be used to read the data from an XML file into a JavaBean. This tutorial will utilize the jox116.jar and dtdparser121.jar libraries and will use the same project from our other tutorial:
The JoxReadJavaBeanFromFile class contains our main method. It reads in the data from the jox-test.xml file and uses that to populate a TestBean object. The method then outputs the contents of the bean to standard output. For brevity, I won't describe the TestBean and TestBean2 classes since they are described in the other tutorial. JoxReadJavaBeanFromFile.javapackage test; import java.io.FileInputStream; import com.wutka.jox.JOXBeanInputStream; public class JoxReadJavaBeanFromFile { public static void main(String[] args) { try { FileInputStream in = new FileInputStream("jox-test.xml"); JOXBeanInputStream joxIn = new JOXBeanInputStream(in); TestBean testBean = (TestBean) joxIn.readObject(TestBean.class); System.out.println("testBean contents:"); System.out.println("testBoolean: " + testBean.isTestBoolean()); System.out.println("testString: " + testBean.getTestString()); TestBean2[] testBean2Array = testBean.getTestBean2Array(); for (int i = 0; i < testBean2Array.length; i++) { TestBean2 testBean2 = testBean2Array[i]; System.out.println(" #" + (i + 1) + " testBean2 String 1: " + testBean2.getTestBean2String1()); System.out.println(" #" + (i + 1) + " testBean2 String 2: " + testBean2.getTestBean2String2()); } } catch (Exception e) { e.printStackTrace(); } } } TestBean is given here: TestBean.javapackage test; import java.io.Serializable; import java.util.Vector; public class TestBean implements Serializable { private static final long serialVersionUID = 1L; private boolean testBoolean; private String testString; private Vector<TestBean2> testVector; public TestBean() { } // Don't use getTestVector... use getTestBean2Array instead // public Vector getTestVector() { // return testVector; // } public TestBean2[] getTestBean2Array() { TestBean2[] testBean2Array = new TestBean2[testVector.size()]; testVector.copyInto(testBean2Array); return testBean2Array; } // Don't use setTestVector... use setTestBean2Array instead // public void setTestVector(Vector testVector) { // this.testVector = testVector; // } public void setTestBean2Array(TestBean2[] testBean2Array) { testVector = new Vector<TestBean2>(); for (int i = 0; i < testBean2Array.length; i++) { testVector.add(testBean2Array[i]); } } public boolean isTestBoolean() { return testBoolean; } public void setTestBoolean(boolean testBoolean) { this.testBoolean = testBoolean; } public String getTestString() { return testString; } public void setTestString(String testString) { this.testString = testString; } } TestBean2 is given here: TestBean2.javapackage test; import java.io.Serializable; public class TestBean2 implements Serializable { private static final long serialVersionUID = 1L; private String testBean2String1; private String testBean2String2; public TestBean2() { } public String getTestBean2String1() { return testBean2String1; } public void setTestBean2String1(String testBean2String1) { this.testBean2String1 = testBean2String1; } public String getTestBean2String2() { return testBean2String2; } public void setTestBean2String2(String testBean2String2) { this.testBean2String2 = testBean2String2; } } The jox-test.xml file consists of the following: jox-test.xml<?xml version="1.0" encoding="ISO-8859-1"?> <JoxTest> <testBean2Array> <testBean2String1>alpha</testBean2String1> <testBean2String2>beta</testBean2String2> </testBean2Array> <testBean2Array> <testBean2String1>gamma</testBean2String1> <testBean2String2>delta</testBean2String2> </testBean2Array> <testBoolean>true</testBoolean> <testString>test bean says hi</testString> </JoxTest> (Continued on page 2) Related Tutorials:
|

