Java Utililty Methods XPath Select

List of utility methods to do XPath Select

  1. HOME
  2. Java
  3. X
  4. XPath Select

Description

The list of methods to do XPath Select are organized into topic(s).

Method

String getValueByXPath(Document document, String xpath)
Get a value by xpath.
String value = "";
try {
 value = xPathFactory.newXPath().evaluate(xpath, document).trim();
} catch (Exception e) {
return value;
int getValueByXPathAsInt(Document document, String xpath)
Get a value by xpath and return as an int.
int x = 0;
try {
 x = Integer.parseInt(getValueByXPath(document, xpath));
} catch (Exception e) {
return x;
String getValueFromXPath(Document document, String xpathString)
Returns the string value of evaluated xpath
try {
 return (String) XPathFactory.newInstance().newXPath().evaluate(xpathString, document,
 XPathConstants.STRING);
} catch (XPathExpressionException e) {
 throw new RuntimeException("Error evaluating xpath [" + xpathString + "] -- " + e.getMessage());
List getXmlElements(Document inXml, String xpath)
get Xml Elements
try {
 NodeList nodeList = (NodeList) xPath.evaluate(xpath, inXml, XPathConstants.NODESET);
 List<Element> results = new ArrayList<>();
 for (int i = 0; i < nodeList.getLength(); i++) {
 results.add((Element) nodeList.item(i));
 return results;
} catch (Exception ex) {
...
ArrayList selectElements(Element element, String xpathExpression)
select Elements
ArrayList<Element> resultVector = new ArrayList<Element>();
if (element == null) {
 return resultVector;
if (xpathExpression.indexOf("/") == -1) {
 NodeList nodeList = element.getChildNodes();
 for (int i = 0; i < nodeList.getLength(); i++) {
 Node node = nodeList.item(i);
...
NodeIterator selectNodeIterator(Node nContextNode, String sXPath)
Use an XPath string to select a nodelist.
try {
 return (NodeIterator) s_mSelectNodeIterator.invoke(null, new Object[] { nContextNode, sXPath });
} catch (Exception e) {
 throw new TransformerException(e);
NodeList selectNodeList(Node contextNode, String expression)
select Node List
XPath xpath = getXPathFactory().newXPath();
XPathExpression xexpr = xpath.compile(expression);
return (NodeList) xexpr.evaluate(contextNode, XPathConstants.NODESET);
NodeList selectNodeList(Node node, String expression)
select Node List
XPathFactory factory = XPathFactory.newInstance();
XPath path = factory.newXPath();
Object result = path.evaluate(expression, node, XPathConstants.NODESET);
return (NodeList) result;
List selectNodes(final Node node, final String xPath)
select Nodes
return wrapNodeList((NodeList) _xpf.newXPath().evaluate(xPath, node, XPathConstants.NODE));
NodeList selectNodes(Node nodeParent, String name)
select Nodes
try {
 XPath xpath = XPathFactory.newInstance().newXPath();
 return (NodeList) xpath.evaluate(name, nodeParent, XPathConstants.NODESET);
} catch (Exception e) {
 return null;


AltStyle によって変換されたページ (->オリジナル) /