XPath从多个节点获取文本

时间:2013-01-16 08:45:54

标签: java xml xpath jaxp

我需要使用nametext创建一个StringArray:

<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);

2 个答案:

答案 0 :(得分:0)

看起来您正在成功检索结果列表,但随后循环遍历它们并在每次迭代期间重新评估XPath。第一次循环遍历result1时,看起来值正确打印出来,所以为什么不直接替换它:

String read=xPath.evaluate("//@name", example);

用这个:

String read = result1.item(i).toString();

答案 1 :(得分:0)

import static javax.xml.xpath.XPathConstants.NODESET;
import static org.apache.commons.lang3.StringUtils.firstNonEmpty;
import static org.apache.commons.lang3.StringUtils.trim;

/**
 * Returns first non-empty result
 * 
 * @param xpaths
 * @return first non-empty result or null if result not found
 */
public static String xpathValue(Document document, String... xpaths) {
    List<String> result = xpathValues(document, xpaths);
    if (result.isEmpty())
        return null;
    if (result.size() == 1)
        return result.get(0);
    throw new IllegalStateException(format("Non-unique result: %s", result));
}

/**
 * Returns first non-empty result
 * 
 * @param xpaths
 * @return first non-empty result or empty list if result not found
 */
public static List<String> xpathValues(Document document, String... xpaths) {
    XPathFactory f = XPathFactory.newInstance();
    return stream(xpaths)
            .map(xpath -> evaluateXpath(document, f, xpath))
            .filter(CollectionUtils::isNotEmpty)
            .findFirst().orElse(emptyList());
}

private static List<String> evaluateXpath(Document document, XPathFactory f, String xpath) {
    try {
        NodeList result = (NodeList) f.newXPath().evaluate(xpath, document, NODESET);
        List<String> liste = new ArrayList<String>(result.getLength());
        for (int i = 0; i < result.getLength(); i++) {
            Node item = result.item(i);
            liste.add(firstNonEmpty(trim(item.getTextContent()), item.getNodeValue()));
        }
        return liste;
    } catch (XPathExpressionException e) {
        throw new IllegalArgumentException("Cannot evaluate xpath: " + xpath, e);
    }
}