Java XPath Get getSpeficValueFromNode(Node n, String xpathExpr)

Here you can find the source of getSpeficValueFromNode(Node n, String xpathExpr)

Description

get Spefic Value From Node

License

Open Source License

Declaration

public static String getSpeficValueFromNode(Node n, String xpathExpr) throws XPathExpressionException 

Method Source Code

//package com.java2s;
/**/*from w  w  w  .  j a  v a 2s  .  c om*/
 * Copyright (c) 2010-2019 Contributors to the openHAB project
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Node;

public class Main {
    private static NamespaceContext ihcNamespaceContext = new NamespaceContext() {
        @Override
        public String getNamespaceURI(String prefix) {
            if (prefix == null) {
                throw new IllegalArgumentException("Prefix argument can't be null");
            } else if ("SOAP-ENV".equals(prefix)) {
                return "http://schemas.xmlsoap.org/soap/envelope/";
            } else if ("ns1".equals(prefix)) {
                return "utcs";
            }
            return "utcs.values";
        }

        @Override
        public String getPrefix(String uri) {
            return null;
        }

        @Override
        @SuppressWarnings("rawtypes")
        public Iterator getPrefixes(String uri) {
            throw new UnsupportedOperationException();
        }
    };

    public static String getSpeficValueFromNode(Node n, String xpathExpr) throws XPathExpressionException {
        XPath xpath = XPathFactory.newInstance().newXPath();
        xpath.setNamespaceContext(ihcNamespaceContext);
        XPathExpression pathExpr = xpath.compile(xpathExpr);
        return (String) pathExpr.evaluate(n, XPathConstants.STRING);
    }
}

Related

  1. getPublicKeyFromKeyInfo(Node keyInfoNode)
  2. getResultXpathstring(String expr, InputSource inputSource)
  3. getScrProperties(String componentName)
  4. getSearchHandlerNode(final File solrconfig)
  5. getSharedXPath()
  6. getStrFromNode(Node xpathnode)
  7. getStrFromNode(Node xpathnode)
  8. getString(final String xPath, final Object item)
  9. getString(Node node, XPathExpression expr)