一千萬個為什麽

搜索

使用XMLDecoder將編碼的XML轉換為List <T>


我正在編寫一個應用程序,它以下列格式讀入大量基本用戶詳細信息;一旦讀入,它允許用戶使用他們的電子郵件搜索用戶的詳細信息:

NAME             ROLE          EMAIL
---------------------------------------------------
Joe Bloggs       Manager       [email protected]
John Smith       Consultant    [email protected]
Alan Wright      Tester        [email protected]
...

我遇到的問題是我需要存儲在公司工作的所有人的大量細節。包含這些詳細信息的文件將每年編寫,僅用於報告目的,但程序需要能夠快速訪問這些詳細信息。

我打算訪問這些文件的方法是讓程序向用戶詢問工作人員的唯一電子郵件的名稱,然後程序從該文件的該行返回名稱和角色。我玩過文本文件,但在搜索這個大文件時,我正在努力處理多列數據。

存儲此類數據的最佳格式是什麽?一個文本文件? XML?尺寸不會打擾我,但我希望能夠盡快搜索它。該文件需要包含大量條目,可能會隨著時間的推移超過10K標記。


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 list = (List) d.readObject();
d.close();
for(Employee x : list) {
    if(x.getEmail().equals(userInput)) {
       //do stuff
    }
}

When the program hits List list = (List) 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?

最佳答案

由於我猜其他人會建議您使用外部數據庫來回答這個問題,我不會:

我建議創建一個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

}

And use the java.beans.XMLDecoder and java.beans.XMLEncoder to serialize/deserialize an ArrayList. (You can read more about them here: http://java.sun.com/j2se/1.4.2/docs/api/java/beans/XMLEncoder.html using an older API, because I don't know which version you use.)

然後,您可以使用foreach搜索此數組:

XMLDecoder d = new XMLDecoder(
               new BufferedInputStream(new FileInputStream("data.xml")));
List list = (List) d.readObject();
d.close();
for(Employee x : list) {
    if(x.getEmail().equals(userInput)) {
       //do stuff
    }
}

使用XML序列化而不是“二進制”序列化的優點是,如果您還為它們提供默認值,您也可以稍後向Employee添加新字段。這使得數據可以靈活地供將來使用。

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

更新:

XMLEncoder/XMLDecoder is a better solution than a binary-serialization. I advice you to do the following.

創建一個新的包裝類:

public class EmployeeList {

    private final ArrayList list = new ArrayList();

    public List getList() {
        return this.list;
    }
    public setList(final List 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);

轉載註明原文: 使用XMLDecoder將編碼的XML轉換為List <T>

猜你喜歡