使用 jxpath 通过特定 属性 查找数组中的元素
Find elements within array, by specific property with 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]
我替换后开始工作了:
Object result = JXPathContext.newContext(new Test()).getValue("countries[gdp=4]");
作者:
Iterator result = JXPathContext.newContext(new Test()).iterate("countries[gdp=4]");
下面代码中的 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]
我替换后开始工作了:
Object result = JXPathContext.newContext(new Test()).getValue("countries[gdp=4]");
作者:
Iterator result = JXPathContext.newContext(new Test()).iterate("countries[gdp=4]");