import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
public class OXMUtil {
public static void main(String args[]) {
ArrayList testArrayList = new ArrayList();
testArrayList.add(new String("Test"));
testArrayList.add(new String("Test1"));
String testString = objectToXML(testArrayList);
System.out.println("XML String .....");
System.out.println(testString);
ArrayList finalArrayList = (ArrayList) XMLToObject(new String(
testString));
System.out.println("Actual Object.....");
for (int i = 0; i < finalArrayList.size(); i++)
System.out.println(finalArrayList.get(i).toString());
}
public static String objectToXML(Object voObj) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
XMLEncoder xmlEncoder = null;
try {
xmlEncoder = new XMLEncoder(new BufferedOutputStream(stream));
xmlEncoder.writeObject(voObj);
xmlEncoder.close();
return stream.toString("UTF-8");
} catch (Exception e) {
System.out.println("Error while Converting from object to XML : "
+ e.getMessage());
xmlEncoder.close();
}
return null;
}
public static Object XMLToObject(String dataXML) {
XMLDecoder xMLDecoder = null;
try {
xMLDecoder = new XMLDecoder(new ByteArrayInputStream(
dataXML.getBytes("UTF-8")));
Object voObj = xMLDecoder.readObject();
xMLDecoder.close();
return voObj;
} catch (Exception e) {
System.out.println("Error while Converting XML to Object : "
+ e.getMessage());
xMLDecoder.close();
}
return null;
}
}
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
public class OXMUtil {
public static void main(String args[]) {
ArrayList
testArrayList.add(new String("Test"));
testArrayList.add(new String("Test1"));
String testString = objectToXML(testArrayList);
System.out.println("XML String .....");
System.out.println(testString);
ArrayList finalArrayList = (ArrayList) XMLToObject(new String(
testString));
System.out.println("Actual Object.....");
for (int i = 0; i < finalArrayList.size(); i++)
System.out.println(finalArrayList.get(i).toString());
}
public static String objectToXML(Object voObj) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
XMLEncoder xmlEncoder = null;
try {
xmlEncoder = new XMLEncoder(new BufferedOutputStream(stream));
xmlEncoder.writeObject(voObj);
xmlEncoder.close();
return stream.toString("UTF-8");
} catch (Exception e) {
System.out.println("Error while Converting from object to XML : "
+ e.getMessage());
xmlEncoder.close();
}
return null;
}
public static Object XMLToObject(String dataXML) {
XMLDecoder xMLDecoder = null;
try {
xMLDecoder = new XMLDecoder(new ByteArrayInputStream(
dataXML.getBytes("UTF-8")));
Object voObj = xMLDecoder.readObject();
xMLDecoder.close();
return voObj;
} catch (Exception e) {
System.out.println("Error while Converting XML to Object : "
+ e.getMessage());
xMLDecoder.close();
}
return null;
}
}
No comments:
Post a Comment