Java – use xmlcoder to convert encoded XML to list

I'm writing an application that reads a lot of basic user details in the following formats; Once read in, it allows users to use their email to search for user details:

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

The problem I encounter is that I need to store a lot of details of everyone working in the company The document containing these details will be prepared annually for reporting purposes only, but the program needs to be able to quickly access these details

I intend to access these files by asking the program to ask the user for the name of the staff's unique e-mail, and then the program returns the name and role from this line of the file I've played with text files, but when searching for this large file, I'm trying to deal with multiple columns of data

What is the best format for storing such data? A text file? XML? The size won't bother me, but I hope to search it as soon as possible The file needs to contain a large number of entries and may exceed 10K tags over time

Editor: I decided to adopt the XML serialization method I've tried to make the encoding code work perfectly, but the following decoding code doesn't 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 clicks list < employee > List = (list < employee >) d.readobject(); Throw an exception saying "employee cannot be converted to Java. Util. List" I've added a reward for this. Anyone who can help me solve this problem once and for all will get a lot of lovely points

Editor 2: I have more views on the problem and have encountered serialization as a potential answer I would appreciate it if someone could investigate for me because I have no experience in serialization or deserialization It can provide an object without any problems, but I really need to return it (list) in the same format as it entered

Editor 3: Well, this problem really started to drive me crazy. To be honest, I began to think it was an unsolvable problem If possible, can someone look at the code and help me provide a solution?

Solution

Since I guess others will suggest that you use an external database to answer this question, I won't:

I suggest creating a java bean, namely

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 Java beans. Xmlcoder and Java beans. Xmlencoder serializes / deserializes ArrayList < employee > (you can read more about them here: http://java.sun.com/j2se/1.4.2/docs/api/java/beans/XMLEncoder.html Use an older API because I don't know which version you're using.)

You can then search this array using 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
    }
}

The advantage of using XML serialization instead of "binary" serialization is that if you also provide default values for them, you can also add new fields to the employee later This makes the data flexible for future use

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

to update:

Xmlencoder / xmldecoder is a better solution than binary serialization I suggest you do the following

Create a new wrapper class:

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) {
        ....
    }

}

You can now use this employeelist as a wrapper Using the following code, you may see what happens when the xmlcoder throws a conversion exception

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.");
}

You must also serialize your employeelist:

EmployeeList el = ...;
XMLEncoder e = new XMLEncoder(...);
e.writeObject(el);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>