|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis article explains how to use Web Objects in XML (WOX) to serialize Java objects to XML. Although WOX is also a serializer for C# objects, this article only concerns the serialization of Java objects. WOX is available at the WOX Serializer website. WOX Main FeaturesSome of the WOX main features are listed below.
Using WOXThis is a quick introduction to the WOX serializer in Java. We will first create two classes. Then we will create some objects of those classes, which will be serialized to XML. Next, we will have a look at the standard XML generated by WOX, and finally we will see how the XML goes back to a Java object.
public class Student {
private String name;
private int registrationNumber;
private Course[] courses;
//constructors and methods omitted
}
public class Course {
private int code;
private String name;
private int term;
//constructors and methods omitted
}
Please notice that the fields in both classes are private. WOX does not take into consideration the visibility of the fields - they will be serialized regardless their visibility. WOX in Java does not require that classes have default constructors, setters, or getters. Serializing the Student object to XMLWe first create a student with some courses. Course[] courses = { new Course(6756, "XML and Related Technologies", 2),
new Course(9865, "Object Oriented Programming", 2),
new Course(1134, "E-Commerce Programming", 3) };
Student student = new Student ("Carlos Jaimez", 76453, courses);
We now use WOX to serialize the student to XML. We need to specify the file name where the student object will be stored. String filename = "student.xml";
Easy.save(student, filename);
The The resulting XML is shown below: <object type="Student" id="0">
<field name="name" type="string" value="Carlos Jaimez" />
<field name="registrationNumber" type="int" value="76453" />
<field name="courses">
<object type="array" elementType="Course" length="3" id="1">
<object type="Course" id="2">
<field name="code" type="int" value="6756" />
<field name="name" type="string" value="XML and Related Technologies" />
<field name="term" type="int" value="2" />
</object>
<object type="Course" id="3">
<field name="code" type="int" value="9865" />
<field name="name" type="string" value="Object Oriented Programming" />
<field name="term" type="int" value="2" />
</object>
<object type="Course" id="4">
<field name="code" type="int" value="1134" />
<field name="name" type="string" value="E-Commerce Programming" />
<field name="term" type="int" value="3" />
</object>
</object>
</field>
</object>
The XML generated is a standard representation for the De-serializing the Student object back from XMLWe will use the Student newStudent = (Student)Easy.load(filename);
Done! The SummaryWeb Objects in XML is an approach to serialize Java objects to XML, in a simple and robust way. The XML generated by WOX aims to be language independent, and easy to understand. In this article we have covered the following: Easy.save(Object obj, String filename). Easy.load(String filename).
The last version of this XML serializer can be found at the WOX serializer website. HistoryRelease 1.0 - May 2008.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||