Total members 11894 |It is currently Fri Nov 22, 2024 3:22 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Using 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 Features

Some of the WOX main features are listed below.
  • Easy to use. The Easy class provides serialization and de-serialization methods.
  • Simple XML. The XML generated is simple, easy to understand, and language independent.
  • Requires no class modifications. Classes do not require to have default constructors, getters or setters.
  • Field visibility. Private fields are serialized just as any other field. WOX serializes fields regardless their visibility.
  • Interoperability Java and C#. WOX can serialize a Java object to XML, and reconstruct the XML back to a C# object; and viceversa.
  • Standard XML object representation. This could potentially allow to have WOX serializers in different object-oriented programming languages.
  • WOX data types. The WOX mapping table specifies how primitive data types are mapped to WOX data types.
  • Robust to class changes. Defaults will be used for newly added fields.
  • Arrays. Handles arrays and multi-dimensional arrays of primitives and Objects.
  • Base-64. Byte arrays are base-64 encoded for efficiency.
  • Collection classes. Lists and Maps are provided as WOX data types.
  • Object references. Handles duplicate and circular object references with id/idref.
  • Class and Type. Objects of these classes are saved by their String name.
  • Small footprint. The woxSerializer.jar file (which contains only .class files) is only 25k.

Using WOX

This 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.

java code
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 XML

We first create a student with some courses.

java code
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.

java code
String filename = "student.xml";
Easy.save(student, filename);


The save method of the Easy class allows you to serialize an object to XML and store it to the specified XML file.

The resulting XML is shown below:

xml code
<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 Student object. Every field is mapped to a field element, and every object is mapped to an object element. Also notice that the type attribute gives you the WOX data type of every field. The XML generated by WOX is simple, easy to understand, and language independent.
De-serializing the Student object back from XML

We will use the load method of the Easy class to de-seralize the Student object.

java code
Student newStudent = (Student)Easy.load(filename);


Done! The Student object has been reconstructed from XML to either Java.
Web 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:
  • Explain the WOX main features.
  • Create an object to be serialized.
  • Serialize the object to XML, by using the method Easy.save(Object obj, String filename).
  • De-serialize the object back from XML, by using the method Easy.load(String filename).




_________________
Please recommend my post if you found it helpful. ,
java,j2ee,ccna ,ccnp certified .


Author:
Expert
User avatar Posts: 838
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Using Web Objects in XML (WOX) to serialize Java objects XML
 Get all objects for an entity     -  
 Smart Objects in photoshop     -  
 Eligible objects for garbage collectors     -  
 what is object's lock and which objects have locks ?     -  
 queue of objects keep track of the front and rear     -  
 invalid argument on IE with script to center objects     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 Java course     -  
 need help in java     -  
 Using FTP in java     -  



Topic Tags

Java Serialization, Java XML
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com