Using XMLDecoder to Pass Coded XML to <T> List

I am writing an application that reads in a large amount of basic user data in the following format; after reading in it, then the user can search for user data using his email address:

NAME             ROLE          EMAIL
---------------------------------------------------
Joe Bloggs       Manager       jbm@company.com
John Smith       Consultant    jsc@company.com
Alan Wright      Tester        awt@company.com
...

The problem I'm suffering with is that I need to store a large number of parts for all the people who worked in the company. A file containing this data will be recorded on an annual basis simply for reporting purposes, but the program will need to be able to quickly access these details.

The way I try to access these files is a program that asks the user for the name of a unique employee email and for the program to then return the name and role from this line of the file, I played with text files, but I struggle with how I will handle multiple columns of data when it comes to finding this large file.

What is the best format for storing such data? Text file? XML? Size does not bother me, but I would like to find it as quickly as possible. The file should contain many entries, possibly with a 10K mark over time.


EDIT: I decided to go with the XML serialization method. I managed to get the code for encoding, which works fine, 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
    }
}

List<Employee> list = (List<Employee>) d.readObject();, , , "Employee java.util.List". , , , .

2: . - , , . - , , ().

EDIT 3: , , , , , . , - ?

+5
5

, , ArrayList, List<Employee>. XMLEncoder ArrayList XML , , XMLDecoder, , Employee .

0

, , , :

Java Bean, ..

public class Employee {

    public String name;
    public String role;
    public String email;

    public Employee() {}

    public Employee(String name, String role, String email) {
        setName(name);
        setRole(role);
        setEmail(email);
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }

    // etc. for other fields

}

java.beans.XMLDecoder java.beans.XMLEncoder / ArrayList<Employee>. ( : http://java.sun.com/j2se/1.4.2/docs/api/java/beans/XMLEncoder.html API, , .)

foreach:

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
    }
}

XML "" , Employee, . .

: http://java.sun.com/products/jfc/tsc/articles/persistence4/

Update:

XMLEncoder/XMLDecoder - , . .

-:

public class EmployeeList {

    private final ArrayList<Employee> list = new ArrayList<Employee>();

    public List<Employee> getList() {
        return this.list;
    }
    public setList(final List<Employee> list) {
        this.list.clear();
        this.list.addAll(list); // shallow copy
    }

    // add your search methods here, for example:
    public Employee getEmployee(String email) {
        ....
    }

}

EmployeeList . , , XMLDecoder, .

XMLDecoder d = new XMLDecoder(
           new BufferedInputStream(new FileInputStream("data.xml")));
final Object o = d.readObject();
System.out.println(o.getClass());
if(o instanceof EmployeeList) {
    EmployeeList el = (EmployeeList) o;

    el.getEmployee(userInput); // TODO
}else{
    System.out.println("Wrong format.");
}

EmployeeList:

EmployeeList el = ...;
XMLEncoder e = new XMLEncoder(...);
e.writeObject(el);
+4

? Derby Hypersonic. . , . . Derby JDK, .

this Derby this Hypersonic.

+2

. , gzipped . , :

 BufferedReader sourceReader = new BufferedReader(new InputStreamReader(
     new GZIPInputStream(new FileInputStream(srcFile))), 4096);

 String line = null;
 while (null != (line = sourceReader.readLine()) {
     String [] colData = line.split("\t");  // alternately use java.util.Scanner 
     // Create maps for columns you want to search on.
 }
 // report results by querying map

, :

   BufferedWriter destinationWriter = new BufferedWriter(new OutputStreamWriter(
       new GZIPOutputStream(new FileOutputStream(destination))));

   // do stuff
   destinationWriter.flush();
   destinationWriter.close();

, ...

+1

, . , , 10K

, XML .

XML ,

  • .
  • RPC .

, , XML .

, db, .

, hsqldb http://hsqldb.org/.

, hsqldb xml ? sql/jdbc/jdo .

XML sql/jdbc/jdo , , hsqldb. , - .

/ / , JDO - . JDO - , .

http://en.wikipedia.org/wiki/Java_Data_Objects
http://www.informit.com/articles/article.aspx?p=212397.

, , jdbc:

Connection c = DriverManager.getConnection("jdbc:hsqldb:file:mydb", "SA", "");
0
source

All Articles