This post shows how to use the Java classes XMLEncoder and XMLDecoder to convert JavaBean objects to XML and back. Note that they support the specifications for JavaBeans to make things work. On the other hand, a conversion may not work if we use objects from Java classes that do not adhere to the JavaBean specification.
Convert a Java object to XML Using XMLEncoder
The following Java codes use the XMLEncoder class to convert a Java object to an XML document.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.turreta.xml; import java.beans.XMLEncoder; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class XmlEncoderTest { public static void main(String[] args) throws FileNotFoundException { StudentBean s = new StudentBean(); s.setLastName("Trump"); s.setFirstName("Donald"); s.setGrade(90); XMLEncoder encoder = new XMLEncoder( new BufferedOutputStream(new FileOutputStream("C:/Users/turreta/Desktop/001/StudentPersisted.xml"))); encoder.writeObject(s); encoder.close(); } } |
This will create an XML file with the file name of StudentPersisted.xml. On closer inspection, we can see that the XML has elements we did not expect, which may render our document non-portable when we use the document on platforms other than Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?xml version="1.0" encoding="UTF-8"?> <java version="1.8.0_60" class="java.beans.XMLDecoder"> <object class="com.turreta.xml.StudentBean"> <void property="firstName"> <string>Donald</string> </void> <void property="grade"> <float>90.0</float> </void> <void property="lastName"> <string>Trump</string> </void> </object> </java> |
Moreover, on the off chance that we rename StudentBean to something else, the XML document becomes invalid to XMLDecoder in the same codebase. The Java XMLDecoder may not convert an XML to an object we desire.
Convert XML to an object Using XMLDecoder
Suppose we have the following XML files and we want to convert its content to a Java object. To convert the XML, we will use the Java XMLDecoder class instead of the XMLEncoder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?xml version="1.0" encoding="UTF-8"?> <java version="1.8.0_60" class="java.beans.XMLDecoder"> <object class="com.turreta.xml.StudentBean"> <void property="firstName"> <string>Hillary</string> </void> <void property="grade"> <float>85</float> </void> <void property="lastName"> <string>Clinton</string> </void> </object> </java> |
Execute the following Java codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.turreta.xml; import java.beans.XMLDecoder; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; public class XmlDecoderTest { public static void main(String[] args) throws FileNotFoundException { XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream("C:/Users/turreta/Desktop/001/hclinton.xml"))); Object object = decoder.readObject(); if (object instanceof StudentBean) { StudentBean student = (StudentBean) object; System.out.println(String.format("Student: %s, %s - %f", student.getLastName(), student.getFirstName(), student.getGrade())); } decoder.close(); } } |
This outputs
1 | Student: Clinton, Hillary - 85.000000 |
This post is part of a reboot Java tutorial.