【发布时间】:2012-12-30 12:41:01
【问题描述】:
我需要创建一个字符串数组,其名称文本来自:
<xs:element name="xyz" type="xs:string/>
<xs:element name="bla" type="xs:string/>
...
如何查询“xyz”、“bla”等?
可能是你见过的最糟糕的代码,但无论如何:
NodeList result1 = (NodeList) xPath.evaluate("//@name", example, XPathConstants.NODESET);
for(int i=0; i<result1.getLength();i++) {
System.out.println("read 1:" +result1.item(i));
}
//console output is:
//read 1:name="xyz"
//read 1:name="bla"
ArrayList<String> liste;
liste = new ArrayList<String>(result1.getLength());
for (int i=0; i<result1.getLength();i++){
String read=xPath.evaluate("//@name", example);
liste.add(read);
System.out.println("read 2: "+read);
}
System.out.println("complete list: " +liste);
//console output is:
//read 2:name="xyz"
//read 2:name="xyz"
//complete list: [xyz, xyz]
感谢您的帮助,这样就可以了:
(以防万一......如果有人在这里寻找解决方案)
NodeList result = (NodeList) xPath.evaluate("//@name", example, XPathConstants.NODESET);
liste = new ArrayList<String>(result.getLength());
for(int i=0; i<result.getLength();i++){
liste.add(result.item(i).getNodeValue());
}
return(liste);
【问题讨论】:
-
我试过了,但它只提供第一项.. 对于上面的例子,它只会写 xyz
-
你的代码在哪里?选择单个节点和多个节点的方法很可能有不同的方法,而您使用的是前者。
-
取决于您使用的是什么,可能您可能会遍历结果集。