In Java objects, how can we perform effective queries? This is always a question worth exploring. You know, in XML, how can we find elements? That's right.
It is a good method. In the same way, I recently discovered that in Java, there is something like XPath, this is the function provided in the jxpath package in the famous Apache Commons library. You can find the relevant download file in http://jakarta.apache.org/commons/jxpath/. (Note: The content of this article mainly comes from
Http://today.java.net/pub/a/today/2006/08/03/java-object-querying-using-jxpath.html
, I have made some changes and profit)
What is jxpath?
First, jxpath can be queried in the class object hierarchy of Java through a simple expression. Let's look at a simple example to illustrate the problem:
For example, in our example, there are some companies with different departments, different employees, and of course the company has their CEOs. employees also have attributes such as name and phone number.
Suppose we want to find out which of the following companies in California have more than 10 employees.
For (iterator companies = database. getcompanies (). iterator (); companies. hasnext ();)
{
// Obtain all companies
Company = (company) companies. Next ();
// Obtain the company in California
If (company. getlocation (). Equals ("CAA ")){
For (iterator attributes = company. getattributes (). iterator (); Attributes. hasnext ();)
{
// Obtain all departments of the company
Department =
(Department) departments. Next ();
If (department. getemployees (). Size ()> 10 ){
System. Out. println (Department );
}
}
}
}
If you use jxpath, you can write it like this.
Iterator attributes = context. iterate (
"/Companies [location = 'CA']" +
"/Orders ments [count (employees)> 10]");
While (attributes. hasnext ()){
System. Out. println (attributes. Next ());
}
As you can see, if jxpath is used, you only need to specify a simple expression, and you will be able to do it right away. Below we will briefly introduce some writing methods.
For example:
More companies:/companies
Obtain departments under the company:/companies/departments
Obtain the company in a certain place:/companies [location = 'CA']. Here, location is the property of companies.
To obtain a company with a development department in the CA, you can:
/Companies [location = 'CA']/deployments [contains (name, 'Development ')]
To obtain more than 10 employees in a department:/companies/departments [count (employees)> 10]
To obtain the second Department of Sun:/companies [name = 'Sun microsystem']/departments [2]
Note that the jxpath sequence number starts from 1, not from 0.
Similar to XPath, it can be like this
/Companies [name = 'Sun microsystem']/
Attributes [last ()]
Next, let's talk about how to use Java code to implement queries.
First, the getter and setter methods must be set for the company, employee, and department classes,
Then, for example
Public list getcompanies ();
You can use
/Companies.
To query all departments under each company, Set
Public list getdocumments ();
Method,
Then, you can use/companies/scripts to query them;
If the Public String getname (); method is provided, it will be called in the following scenarios:
/Companies/deployments [name = 'server development']
For example, assume that a map has been set to store someone's phone number, for example, a work phone number or a private phone number:
Map numbers = employee. gettelephonenumbers ();
String worknumber =
(String) numbers. Get ("work number ");
The above is an employee's call, but jxpath must be written in this way.
.../Employees [1]/
Telephonenumbers [@ name = 'work number']
@ Name is used to specify the key in the map, instead of simply writing the above
.../Employees [1]/
Telephonenumbers/work number
Finally, we can use jxpath together. First, declare the root object. That is to say, you must first construct a class hierarchy. For example, you have already constructed a class hierarchy called
Database Class, then:
Jxpathcontext context = jxpathcontext.
Newcontext (database );
The database class contains all the company instances. The context is extracted first. Then,
Iterator attributes = context. iterate ("/companies [location = 'wa ']" +
"/Orders ments [count (employees)> 10]");
Then, we will retrieve a collection of all the companies with over 10 employees in the WA Department.
Final output:
While (attributes. hasnext ()){
Department dept = (department) I. Next ();
}
Look, it's easy.
Finally, we strongly recommend you download the example in this article to see it. You can just run it in eclipse and write it clearly.
Code: http://today.java.net/today/2006/08/03/source_code.tar.gz