views:

27

answers:

2

Hi,

I am trying to use the xmlEncoder to write to xml file in net-beans but it doesnt work.

Here is the call to the writing function:

dbManipulator.writeStudents(deps);

where

deps = new Hashtable<String, Department>();
dbManipulator = new DataBaseManipulator();

Department is an class-object I made, and here is writeStudents method which is located in the DataBaseManipulator class:

 public void writeStudents(Hashtable<Integer, Student> students)
    {
            XMLEncoder encoder = null;
            try
            {
                encoder = new XMLEncoder(new FileOutputStream(".\\test\\Students.xml"));
            }
            catch(Exception e){}
            encoder.writeObject(students);
            encoder.close();
    }//end of function writeStudents()

Any ideas why it isnt working? I tried changing the hashtable to vector but still the xml file looks like that after the writing:

<?xml version="1.0" encoding="UTF-8"?> 
<java version="1.6.0_18" class="java.beans.XMLDecoder"> 
 <object class="java.util.Hashtable"/> 
</java> 

Thanks in advance,

Greg

A: 

Is Students following Java Beans spec? Don't forget that if your object only has default data, nothing will be written except an element representing that there is such an object. That's because the encoder doesn't write any data that the default constructor can take care of.

Check if your hashTable really contains student objects.

ring bearer
`deps = new Hashtable<String, Department>();` `dbManipulator.writeStudents(deps);` and then: `public void writeStudents(Hashtable<Integer, Student> students)`, it shouldn't compile.
Pindatjuh
A: 

This is how the Student class looks:

package Application;
import java.util.*;

public class AcceptedStudent extends Student{

    private String depName;
    private Hashtable<String, CourseDetails> coursesDetails; // key - courseName string, value - courseDetails object

    public AcceptedStudent(int newId, String first, String last, String newDep)
    {
        super(newId, first, last);
        depName = newDep;
        coursesDetails = new Hashtable<String, CourseDetails>();
    }

    public AcceptedStudent(int newId, String first, String last, String newDep, Hashtable<String, CourseDetails> newCourseDetails)
    {
        super(newId, first, last);
        depName = newDep;
        coursesDetails = newCourseDetails;
    }

     // Function that checks if the student took the course and got higher than 56
    public boolean checkSuccessInCourse(String courseName)
    {
        // If the student took the pre course
        if (coursesDetails.containsKey(courseName))
        {
            // If the student got grade higher than 56 in this course
            if (((CourseDetails)coursesDetails.get(courseName)).getGrade() >= 56)
            {
                return true;
            }
            return false;
        }
        return false;
    }

    public void addCourseDetails(CourseDetails cd)
    {
        coursesDetails.put(cd.getCourseName(), cd);
    }

    public Hashtable getCourseDetails()
    {
        return coursesDetails;
    }
    public String getDep()
    {
        return depName;
    }
}

and Student class is:

package Application;

public class Student {

    private int id;
    private String fName;
    private String lName;
    private boolean status;


    public Student(int newId, String first, String last)
    {
        id = newId;
        fName = first;
        lName = last;
        status = false;
    }

    public int getId()
    {
        return id;
    }

    public String getFirstName()
    {
        return fName;
    }

    public String getLastName()
    {
        return lName;
    }

    public boolean getStatus()
    {
        return status;
    }


}
Greg