0% found this document useful (0 votes)
48 views6 pages

Le3 Serialization

The document discusses serialization, which is converting an object's state into a form that can be persisted or transported. It provides an example of creating Book, Author, and PubDate classes, adding objects of these to an ArrayList, and serializing the ArrayList to an XML file using XMLEncoder.

Uploaded by

Migz N Yumz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views6 pages

Le3 Serialization

The document discusses serialization, which is converting an object's state into a form that can be persisted or transported. It provides an example of creating Book, Author, and PubDate classes, adding objects of these to an ArrayList, and serializing the ArrayList to an XML file using XMLEncoder.

Uploaded by

Migz N Yumz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LE3- SERIALIZATION

Serialization is the process of converting the state of an object into a form that
can be persisted or transported (network).

1. Create the following object:

public class Book{


private String bookTitle;
private Author name;
private PubDate date:
}

public class PubDate{


private int year;
private String month;
private int day;
}

public class Author{


private String firstname;
private String lastname;
private String title;
}

2. Create an ArrayList that will store 5 books.

3. Serialize the ArrayList object.

4. Check to see if you were able to produce the XML file correctly.
ANSWER:

Type or code the following (NetBeans, Java, IntelliJ – JetBrains).

Book.java
public class Book {
private String title;
private Author author;
private PubDate pubDate;

public Book(String title, Author author, PubDate pubDate) {


this.title = title;
this.author = author;
this.pubDate = pubDate;
}

public Book() {}

public String getTitle() {


return title;
}

public void setTitle(String title) {


this.title = title;
}

public Author getAuthor() {


return author;
}

public void setAuthor(Author author) {


this.author = author;
}

public PubDate getPubDate() {


return pubDate;
}

public void setPubDate(PubDate pubDate) {


this.pubDate = pubDate;
}
}
Author.java
public class Author {
private String firstname;
private String lastname;

public Author(String firstname, String lastname) {


this.firstname = firstname;
this.lastname = lastname;
}

public Author() {}

public String getFirstname() {


return firstname;
}

public void setFirstname(String firstname) {


this.firstname = firstname;
}

public String getLastname() {


return lastname;
}

public void setLastname(String lastname) {


this.lastname = lastname;
}
}

PubDate.java
public class PubDate {
private int year;
private int month;
private int day;

public PubDate(int year, int month, int day) {


this.year = year;
this.month = month;
this.day = day;
}

public PubDate() {}

public int getYear() {


return year;
}

public void setYear(int year) {


this.year = year;
}

public int getMonth() {


return month;
}
public void setMonth(int month) {
this.month = month;
}

public int getDay() {


return day;
}

public void setDay(int day) {


this.day = day;
}
}

Main.java
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or


// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {

public static void main(String[] args) throws FileNotFoundException {


// Create first book
Book book1 = new Book("Harry Potter",
new Author("JK", "Rowling"),
new PubDate(1990, 1, 25));

// Create second book


Book book2 = new Book("Lord of the Rings",
new Author("JRR", "Tolkien"),
new PubDate(1980, 2, 14));

// Create arraylist to store book objects


ArrayList<Book> books = new ArrayList<>();

// Add books to arraylist


books.add(book1);
books.add(book2);

// Serialize arraylist to xml


XMLEncoder encoder=null;
// Create xml encoder/serializer, specify filename, relative or
absolute path
encoder = new XMLEncoder(new FileOutputStream("books.xml"));
// Pass in arraylist of book objects to encoder
encoder.writeObject(books);
// Close encoder to flush objects to file
encoder.close();
}
}
OUTPUT:

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>

You might also like