public long getCountValue(final String xPath, final Object... params) { String xpathString = format(xPath, params); try { XPathExpression xPathExpression = createXPathExpression(xpathString); Double number = (Double) xPathExpression.evaluate(rootElement, XPathConstants.NUMBER); return number.intValue(); } catch (XPathExpressionException e) { throw new RuntimeException(e); } }
/** * This method never returns null. * * @param xPath * @param params * @return {@code String} value or empty string */ public String getValue(final String xPath, final Object... params) { String xPath_ = format(xPath, params); NodeList nodeList = getNodeList(rootElement, xPath_); if (nodeList.getLength() == 1) { Node node = nodeList.item(0); if (node.getNodeType() != Node.ELEMENT_NODE) { String value = nodeList.item(0).getTextContent(); return value.trim(); } } return ""; }
public XmlDom getElement(final String xPath, final Object... params) { try { String xPath_ = format(xPath, params); NodeList nodeList = getNodeList(rootElement, xPath_); for (int ii = 0; ii < nodeList.getLength(); ii++) { Node node = nodeList.item(ii); if (node != null && node.getNodeType() == Node.ELEMENT_NODE) { return new XmlDom((Element) node); } } return null; } catch (Exception e) { String message = "XPath error: '" + xPath + "'."; throw new DSSException(message, e); } }
/** * The list of elements corresponding the given XPath query and parameters. * * @param xPath * @param params * @return */ public List<XmlDom> getElements(final String xPath, final Object... params) { try { String xPath_ = format(xPath, params); NodeList nodeList = getNodeList(rootElement, xPath_); List<XmlDom> list = new ArrayList<XmlDom>(); for (int ii = 0; ii < nodeList.getLength(); ii++) { Node node = nodeList.item(ii); if (node != null && node.getNodeType() == Node.ELEMENT_NODE) { list.add(new XmlDom((Element) node)); } } return list; } catch (Exception e) { String message = "XPath error: '" + xPath + "'."; throw new DSSException(message, e); } }