Question

In: Computer Science

1. Assume you are writing code to serialize a group of Student objects to an XML...

1. Assume you are writing code to serialize a group of Student objects to an XML document. What Java API method will you call to execute the serialization process? NOTE: You must use the XML serialization/deserialization method/concepts presented in this course.

2. Assume that the list of exception types below may occur within the scope of a method. In order to properly handle these exceptions, each type must be handled individually and separately.  Arrange the exception types in the order in which your code must address them to insure that no type "falls through" to be processed along with any other type. (HINT: These are all Java API exception types.)

5 points

IOException

Exception

FileSystemException  

NoSuchFileException

Solutions

Expert Solution

1. To Serialize an object to XML we need to Create an XMLEncoder Class Object which is available in "java.beans" package. After Creating the Object we can use writeObject() to encode the Object.

To Better Understand the Concept Lets consider the following Student Class:

Student.java

import java.io.*;
import java.util.*;

public class Student {  
   private String name;
   private String HNo;
   private float gpa;
  
   public Student(){}  

   public Student(String name, String Hno,float gpa) {      
       this.name = name;
       this.HNo = Hno;
       this.gpa = gpa;
  
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
  
   public String getHNo() {
       return HNo;
   }
   public void setHNo(String Hno) {
       this.HNo = Hno;
   }
   public float getGpa() {
       return gpa;
   }
   public void setGpa(float gpa) {
           this.gpa = gpa;
   }
}

We Use the Following SerializeToXML class that use the XMLEncoder to Encode Objects to XML files

import java.io.*;
import java.util.*;
import java.beans.XMLEncoder;
public class SerializeToXML {  
   private static final String SERIALIZED_FILE_NAME="student.xml";
   public static void main(String args[]){
       Student s1=new Student("John","123",3.7f);
       Student s2=new Student("Smith","124",3.7f);
       Student s3=new Student("Ben","125",3.7f);
       Student s4=new Student("Tom","126",3.7f);
       List sList=new ArrayList();
       sList.add(s1);
       sList.add(s2);
       sList.add(s3);
       sList.add(s4);
       XMLEncoder encoder=null;
       try{
       encoder=new XMLEncoder(new BufferedOutputStream(new FileOutputStream(SERIALIZED_FILE_NAME)));
       }catch(FileNotFoundException fileNotFound){
           System.out.println("ERROR: While Creating or Opening the File dvd.xml");
       }
       encoder.writeObject(s1);
       encoder.writeObject(s2);
       encoder.writeObject(s3);
       encoder.writeObject(s4);
       encoder.close();

   }

}

Output:

After Execution the XML file is Generated in the Present Working Directory.

The Created XML file is:

2. The relationship between The four Exceptions is in the following Hierarchy.

  • java.lang.Exception
    • java.io.IOException
      • java.nio.file.FileSystemException
        • java.nio.file.NoSuchFileException

So the Correct order to Handel the Four exceptions is First  NoSuchFileException, then FileSystemException, then IOException and finally Exception


Related Solutions

XML and XSL I'm trying to style my XML code but it's not implementing the XSL...
XML and XSL I'm trying to style my XML code but it's not implementing the XSL file when I run it even though the file is referenced. Any help? XML code: <?xml version="1.0"?> <?xml-stylesheet href="textbooks.xsl" type="text/xsl" ?> <textbooks> <textbook> <title> Introduction to Design and Analysis of Algorithms</title> <authors> <author> <firstName>Anany</firstName> <lastName>Levitin</lastName> </author> </authors> <publisher> <name>Ed, Pearson</name> <website>https://www.pearson.com</website> </publisher> <Year-of-Publication>2011</Year-of-Publication> <ISBN>978-0132316811</ISBN> <book-specific-website></book-specific-website> <edition>3rd edition</edition> <cover-type>Paperback</cover-type> </textbook> <textbook> <title>Software Engineering: A Practitioner’s Approach</title> <authors> <author> <firstName>Roger</firstName> <lastName>Pressman</lastName> </author> </authors> <publisher> <name>Ed, McGraw-Hill</name>...
you will develop a small XML. XML is used for storing and exchanging information. XML is...
you will develop a small XML. XML is used for storing and exchanging information. XML is the most commonly used file format for e-business. It is important to have a grasp of XML. In XML, you can choose your own tag names. For example, for representing name of a product, you can use <pname>, <prodName> or <productname> as you like. XML has to follow certain syntax rules.  Gentle Introduction to XML (External Link) provides more information about XML. Do the following:...
Android Studio Code: Provide a working android studio code i.e java and xml code for the...
Android Studio Code: Provide a working android studio code i.e java and xml code for the activity below: Develop an application that is capable to turn pages back and forth. Detailed Instructions: For the main activity, create a layout that represents a fictitious title and author. The activity should include a clickable button on the bottom right that allows you to go forward to the next activity. The next activity will simply be an image above simple text that can...
Assume that student are to be sperated into a group with IQ scores in the bottom...
Assume that student are to be sperated into a group with IQ scores in the bottom 30%, a second group with scores in the middle 40%, and a third group with scores in the top 30%. The Wechsler Adult Intelligence Scale yields an IQ score obtained through a test, and the scores are normally distributed with a mean of 100 and a standard deviation of 15. Find the IQ score that seperates the three groups.
This is an XML exercise. Explain why you need LINQ to XML.
This is an XML exercise. Explain why you need LINQ to XML.
In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
Explain how business websites can benefit from the different objects PHP offer that deal with XML....
Explain how business websites can benefit from the different objects PHP offer that deal with XML. Using appropriate research and readings, explain how you, as a developer, can use the objects or components, to improve a business website. In many situations, websites store user data in XML format. Write a function or procedure that reads user input (say name, e-mail address, and comment) from the screen or form and stores them in a file in XML format. Define the XML...
Program #1: You will be writing code for several different examples of conditional execution. Each example...
Program #1: You will be writing code for several different examples of conditional execution. Each example should be written as a unique method. Each method should include a method header. So the basic outline of your code is: public class Branching { /** * This is an example method header. Notice how it's multi-line and written * using college-level English and not fragments - this is for the Human to * read and not the compiler to parse, so be...
Pretend you are writing a SQL Code for a database with aHomeowners Insurance Company.Use...
Pretend you are writing a SQL Code for a database with a Homeowners Insurance Company.Use Microsoft SQL to write a code for the following:Create a new table called Policy_Total. It will contain the number of policies in the policy table. Create a trigger that increments the policy total every time a policy is added. Create another trigger that decrements the policy total every time a policy is deleted.Create a stored procedure that compares the policy total field from the Policy...
R problem 1. Student records. Create an S3 class studentRecord for objects that are a list...
R problem 1. Student records. Create an S3 class studentRecord for objects that are a list with the named elements ‘name’, ‘subjects completed’, ‘grades’, and ‘credit’. Write a studentRecord method for the generic function mean, which returns a weighted GPA, with subjects weighted by credit. Also write a studentRecord method for print, which employs some nice formatting, perhaps arranging subjects by year code. Finally create a further class for a cohort of students, and write methods for mean and print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT