簡體   English   中英

XPath 從多個節點獲取文本

[英]XPath get text from multiple nodes

我需要使用以下名稱創建一個 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);

看起來您正在成功檢索結果列表,但隨后您會遍歷它們並在每次迭代期間重新評估 XPath。 第一次循環遍歷 result1 時,這些值似乎正確打印出來,那么為什么不替換它:

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

有了這個:

String read = result1.item(i).toString();
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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM