Le3 Serialization
Le3 Serialization
Serialization is the process of converting the state of an object into a form that
can be persisted or transported (network).
4. Check to see if you were able to produce the XML file correctly.
ANSWER:
Book.java
public class Book {
private String title;
private Author author;
private PubDate pubDate;
public Book() {}
public Author() {}
PubDate.java
public class PubDate {
private int year;
private int month;
private int day;
public PubDate() {}
Main.java
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
books.xml
<?xml version="1.0" encoding="UTF-8"?>
<java version="22" class="java.beans.XMLDecoder">
<object class="java.util.ArrayList">
<void method="add">
<object class="Book">
<void property="author">
<object class="Author">
<void property="firstname">
<string>JK</string>
</void>
<void property="lastname">
<string>Rowling</string>
</void>
</object>
</void>
<void property="pubDate">
<object class="PubDate">
<void property="day">
<int>25</int>
</void>
<void property="month">
<int>1</int>
</void>
<void property="year">
<int>1990</int>
</void>
</object>
</void>
<void property="title">
<string>Harry Potter</string>
</void>
</object>
</void>
<void method="add">
<object class="Book">
<void property="author">
<object class="Author">
<void property="firstname">
<string>JRR</string>
</void>
<void property="lastname">
<string>Tolkien</string>
</void>
</object>
</void>
<void property="pubDate">
<object class="PubDate">
<void property="day">
<int>14</int>
</void>
<void property="month">
<int>2</int>
</void>
<void property="year">
<int>1980</int>
</void>
</object>
</void>
<void property="title">
<string>Lord of the Rings</string>
</void>
</object>
</void>
</object>
</java>