Java Utililty Methods XPath Get

List of utility methods to do XPath Get

Description

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

Method

Stringget(String path, Node e)
get
return (String) evaluateXPath(path, e, XPathConstants.STRING);
StringgetAttribute(String fileName, String xPathExpression, String attributeName)
get Attribute
try {
    Document document = parseXML(new File(fileName));
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression xpathExpression = xpath.compile(xPathExpression);
    Node node = (Node) xpathExpression.evaluate(document, NODE);
    return getAttributeValueByName(node, attributeName);
} catch (Exception e) {
    throw new RuntimeException("Failed to extract element from:" + fileName, e);
...
StringgetBaseLocation(byte[] metadata)
get Base Location
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
    builder = builderFactory.newDocumentBuilder();
    Document document = builder.parse(new ByteArrayInputStream(metadata));
    return (String) xPath.compile(BASE_LOCATION).evaluate(document, XPathConstants.STRING);
} catch (XPathExpressionException | ParserConfigurationException | SAXException | IOException e) {
    return null;
...
booleangetBool(Node node, boolean def, XPathExpression expr)
Get a boolean from an XPath expression.
String s = getStringOrNull(node, expr);
if (s == null) {
    return def;
return s.equalsIgnoreCase("true");
MapgetCache()
get Cache
Map<String, XPathExpression> mRetVal = sCache.get();
if (mRetVal == null) {
    mRetVal = new HashMap<String, XPathExpression>();
    sCache.set(mRetVal);
return mRetVal;
X509CertificategetCertificateFromKeyInfo(Node keyInfoNode)
get Certificate From Key Info
X509Certificate cert = null;
try {
    String s = x509Path.evaluate(keyInfoNode);
    if (s == null || s.length() == 0)
        return null;
    byte[] decoded = Base64.decodeBase64(s);
    cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(decoded));
} catch (XPathExpressionException e) {
...
XPathgetCruxPagesXPath()
get Crux Pages X Path
XPathFactory factory = XPathFactory.newInstance();
XPath findPath = factory.newXPath();
findPath.setNamespaceContext(new NamespaceContext() {
    public String getNamespaceURI(String prefix) {
        if (prefix.equals("c")) {
            return "http://www.cruxframework.org/crux";
        } else if (prefix.equals("v")) {
            return "http://www.cruxframework.org/view";
...
XPathgetDefaultXPath()
get Default X Path
return defaultXPath;
XPathFactorygetDefaultXPathFactory()
get Default X Path Factory
return s_aXPathFactory;
StringgetDigestValue(Node reference)
Returns the value of DigestValue node
return getNodeValue(reference, GET_DIG_VAL_EXP);