java - 通过 jxpath 的特定属性在数组中查找元素
问题描述
下面代码中的 JXpath 表达式有什么问题?下面的代码总是抛出这个异常:
org.apache.commons.jxpath.JXPathNotFoundException: No value for xpath: countries[gdp=4]
但是我希望这个国家德国会回来。这里有什么问题?
import org.apache.commons.jxpath.JXPathContext;
public class Test {
private final Country[] countries = {
new Country("US", 20),
new Country("China", 13),
new Country("Japan", 5),
new Country("Germany", 4),
new Country("UK", 3)};
public static void main(String[] args) {
Object result = JXPathContext.newContext(new Test()).getValue("countries[gdp=4]");
System.out.println(result);
}
public Country[] getCountries() {
return countries;
}
}
-
class Country{
private String name;
private int gdp;
public Country(String name, Integer gdp){
this.name=name;
this.gdp=gdp;
}
@Override
public String toString() {
return name;
}
public String getName() {
return name;
}
public int getGdp() {
return gdp;
}
}
解决方案
我对您正在使用的 jxpath 库知之甚少。但是 xpath 表达式应该是这样的:
/countries/country[gdp=4]
推荐阅读
- c# - 如何拆分字符串,保持顺序和拆分原因?
- mysql - Node Socket 服务器性能很慢
- flutter - 如何在颤动中处理不同尺寸的图像?
- python - 将 JSON 文件转换为 Pandas 数据
- r - 在 R 和 Leaflet 中悬停时添加弹出到标记簇
- python - VS Code 交互式窗口浏览器
- sql - 如何在 Oracle SQL 中选择不同的多列值?
- spring-boot - SpringBoot Docker REST Connection Refused 即使创建了 docker-compose
- typescript - typescript 处理不同类型的返回
- sql - 如何在记事本++中的一个大sql行中将特定字符之后的某些字符替换为另一个特定字符