Here you can find the source of getXPathValue(XPath xpath, Node d, String xq, String def)
public static String getXPathValue(XPath xpath, Node d, String xq, String def)
//package com.java2s; //License from project: Open Source License import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static String getXPathValue(XPath xpath, Node d, String xq, String def) { if (xq.isEmpty()) { return def; }//from w w w . j a va 2s.co m StringBuilder sb = new StringBuilder(); try { NodeList nl = (NodeList) xpath.evaluate(xq, d, XPathConstants.NODESET); for (int i = 0; i < nl.getLength(); i++) { String s = nl.item(i).getTextContent(); if (s == null) { continue; } if (s.trim().isEmpty()) { continue; } if (sb.length() > 0) { sb.append("; "); } sb.append(s); } if (sb.length() == 0) { return def; } return sb.toString(); } catch (XPathExpressionException e) { e.printStackTrace(); } return def; } }