91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java object queries using JXPath

發布時間:2020-08-07 13:51:24 來源:ITPUB博客 閱讀:125 作者:Sky-Tiger 欄目:編程語言

Company

Department

Employee (name, job title, age)

Acme Inc.

Sales

Johnny, Sales rep, 45

Sarah, Sales rep, 33

Magda, Office assistant, 27

Accounting

Steve, Head controller, 51

Peter, Assistant controller, 31

Susan, Office assistant, 27

With that in place, let's start using JXPath!

Executing simple JXPath queries

The simplest query possible extracts a single object from the object tree. For example, to retrieve Company, use the following code:

JXPathContext context = JXPathContext.newContext(company);
Company c = (Company)context.getValue(".");

The first line shows the creation of a context, the starting point for all JXPath's XPath expressions in the object tree (comparable to the rootnode element in an XML document). The second line of code executes the actual query. Since our context starts at the company level, to retrieve the Company object, we simply use the current-element selector '.'.

Using predicates and variables

An Employee is a child object of a Department. To retrieve the Employee named "Johnny" use the following code (Company is still context's starting point):

Employee emp = (Employee)context.getValue("/departmentList/employees[name='Johnny']");

Basically, the code reads: "Search all Departments from the beginning for the Employee object of which the name attribute has the value 'Johnny'."

The above code snippet illustrates how to use a predicate to search objects by using particular values. Using predicates is comparable to using the WHERE clause in SQL. We can even combine multiple predicates in one query:

Employee emp = 
   (Employee)context.getValue("/departmentList/employees[name='Susan' and age=27]");

Unless you're using an ad-hoc, one-time-only query, implementing hard-coded queries usually isn't feasible. It is better to define a reusable query that you can then execute with different parameters. To accommodate parameterized querying, JXPath supports variables in queries. Using variables, the code above now looks like this:

 context.getVariables().declareVariable("name", "Susan");
context.getVariables().declareVariable("age", new Integer(27));
Employee emp = 
   (Employee)context.getValue("/departmentList/employees[name=$name and age=$age]");

Iterating over collections

JXPath can provide an iterator over all objects retrieved by a query, much like iterating a result-set. The following snippet shows how you can iterate over all Departments:

 for(Iterator iter = context.iterate("/departmentList"); iter.hasNext();){
   Department d = (Department)iter.next();
   //...
}

To retrieve all Employees from all Departments and iterate over them:

 for(Iterator iter = context.iterate("/departmentList/employees"); iter.hasNext();){
   Employee emp = (Employee)iter.next();
   //...
}

To retrieve all Employees older than 30 years from the sales department:

 for(Iterator iter = context.iterate
     ("/departmentList[name='Sales']/employees[age>30]"); iter.hasNext();){
   Employee emp = (Employee)iter.next();
   //...
}

And the above example with variables:

 context.getVariables().declareVariable("deptName", "Sales");
context.getVariables().declareVariable("minAge", new Integer(30));
for(Iterator iter = context.iterate("/departmentList
     [name=$deptName]/employees[age>$minAge]"); iter.hasNext();){
   Employee emp = (Employee)iter.next();
   //...
}

Those two last code snippets also demonstrate the use of several predicates within one XPath query.

Pointers

A Pointer is a JXPath utility object that represents a reference to the location of an object in the object tree. For example, a Pointer might refer to "the first employee of the second department." Compared to objects retrieved directly from the tree, Pointers offer additional functions like the execution of relative queries through relative contexts (more on this later).

Using Pointers

Having a Pointer refer to an object in the object tree is almost identical to directly retrieving objects:

 JXPathContext context = JXPathContext.newContext(company);
Pointer empPtr = context.getPointer("/departmentList[name='Sales']/employees[age>40]");

System.out.println(empPtr);
//output: /departmentList[1]/employees[1]

System.out.println(((Employee)empPtr.getValue()).getName());
//output: Johnny

Note that the Pointer's output demonstrates that a Pointer describes an object's location, rather than the object itself. Also note that the actual object the Pointer refers to can be retrieved through the Pointer's getValue() method.

Pointers can also be iterated over, as the following snippet demonstrates:

 for(Iterator iter = context.iteratePointers("/departmentList[name='Sales']
     /employees[age>30]"); iter.hasNext();){
   Pointer empPtr = (Pointer)iter.next();
   //...
}

Relative context and relative queries

Since a Pointer describes a location, it can be used as a reference point for navigating through the entire object tree. To do that, use the Pointer as the root object (Remember using the Company object for that earlier?) in a so called relative context. From this relative context, you can query the entire object tree by executing relative queries. This advanced use of Pointers offers great flexibility as the examples below illustrate.

To begin, here's how you create a relative context:

 for(Iterator iter = context.iteratePointers("/departmentList[name='Sales']
     /employees[age>30]"); iter.hasNext();){
   Pointer empPtr = (Pointer)iter.next();
   JXPathContext relativeContext = context.getRelativeContext(empPtr);
}

In this code snippet, a new relative context is created for consecutive employee pointers.

Using the relative context, XPath queries can be executed on the entire object tree of siblings, children, and parent/grandparent objects, as the following snippet demonstrates:

 //Current employee
Employee emp = (Employee)relativeContext.getValue(".");

//Employee name
String name = (String)relativeContext.getValue("./name");

//Name of the Department this Employee belongs to (a parent object)
String deptName = (String)relativeContext.getValue("../name");

//Name of the Company this Employee belongs to (a 'grandparent' object)
String compName = (String)relativeContext.getValue("../../name");

//All coworkers of this Employee (sibling objects)
for(Iterator empIter = relativeContext.iterate("../employees"); empIter.hasNext();){
   Employee colleague = (Employee)empIter.next();
   //...
}

Summary

JXPath is an extremely useful tool for traversing, navigating, and querying complex object trees. Because it uses the XPath expression language for its queries, a large body of reference material is available to help you build efficient yet complex object-retrieval queries. Even more flexibility is added by using Pointers and relative contexts.

This brief article only scratches the surface of JXPath's possibilities, for a more in depth discussion with more advanced usage examples, read my full tutorial.

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

丹江口市| 木里| 措美县| 无锡市| 天峻县| 隆德县| 上犹县| 石林| 龙口市| 格尔木市| 万载县| 苍溪县| 阜新| 沛县| 连城县| 罗江县| 鄯善县| 肃宁县| 延吉市| 德格县| 扎囊县| 廉江市| 济源市| 惠来县| 龙泉市| 黑河市| 固原市| 南充市| 宣城市| 偏关县| 和政县| 城口县| 新民市| 高陵县| 济阳县| 甘肃省| 什邡市| 花莲市| 晋江市| 习水县| 吴旗县|