JXPath simplifies Java queries

Consider a company that contains many departments. Those departments contain employees, those employees have names, telephone numbers, and so on. To query a collection of companies and find, for example, all departments of companies in California with more than 10 employees, you could write something like this:

Using JXPath you can simplify this:

Two points should be noted from this example:

  • The code using JXPath is considerably shorter. The query is concisely expressed in one statement.
  • You can take that expression out of the code and parameterize it, or put it in a configuration file.

Perfect for Unit Testing

Unit testing often requires checking object hierarchies for particular values. You can use JXPath to express the components to be tested. For example, you can write a helper method in a JUnit test:

and then you can use this in tests:

Read more in this java.net article.

This Post Has One Comment

  1. exceptionell plats inlägget. Jag ska bokmärke och kolla in mycket oftare. Jag gillar verkligen webbplats mall

Leave a Reply

Close Menu