Using XMLDecoder to cast Encoded XML to List<>
Posted
by Ender
on Stack Overflow
See other posts from Stack Overflow
or by Ender
Published on 2010-04-16T00:13:05Z
Indexed on
2010/04/20
9:13 UTC
Read the original article
Hit count: 518
java
|data-handling
I am writing an application that reads in a large number of basic user details in the following format; once read in it then allows the user to search for a user's details using their email:
NAME ROLE EMAIL
---------------------------------------------------
Joe Bloggs Manager [email protected]
John Smith Consultant [email protected]
Alan Wright Tester [email protected]
...
The problem I am suffering is that I need to store a large number of details of all people that have worked at the company. The file containing these details will be written on a yearly basis simply for reporting purposes, but the program will need to be able to access these details quickly.
The way I aim to access these files is to have a program that asks the user for the name of the unique email of the member of staff and for the program to then return the name and the role from that line of the file. I've played around with text files, but am struggling with how I would handle multiple columns of data when it comes to searching this large file.
What is the best format to store such data in? A text file? XML? The size doesn't bother me, but I'd like to be able to search it as quickly as possible. The file will need to contain a lot of entries, probably over the 10K mark over time.
EDIT: I've decided to go with the XML serialisation method. I've managed to get the code for Encoding working perfectly, but the Decoding code below does not work.
XMLDecoder d = new XMLDecoder(
new BufferedInputStream(new FileInputStream("data.xml")));
List<Employee> list = (List<Employee>) d.readObject();
d.close();
for(Employee x : list) {
if(x.getEmail().equals(userInput)) {
// do stuff
}
}
When the program hits List<Employee> list = (List<Employee>) d.readObject();
an exception is thrown claiming that "Employee cannot be cast to java.util.List". I've added a bounty to this and anyone that can help me solve this problem once and for all will get lots of lovely points.
EDIT 2: I've looked a bit more into the problem and have come across Serialization as a potential answer. If anyone can look into this for me as I've no experience with Serialization or Deserialization I'd be very grateful. It can provide an Object with no problems whatsoever, but I really need to return it in the same format as it went in (List).
EDIT 3: Ugh, this problem is really starting to drive me crazy and to be honest I'm starting to think that it's an unsolvable problem. If possible, could someone take a look at the code and help provide a solution for me?
© Stack Overflow or respective owner