Java XPath Expression parseXMLValue(String xml, String xpathExpression)

Here you can find the source of parseXMLValue(String xml, String xpathExpression)

Description

parse XML Value

License

Open Source License

Declaration

public static String parseXMLValue(String xml, String xpathExpression)
            throws IOException, XPathExpressionException 

Method Source Code

//package com.java2s;
/**/*from   w  ww.  ja v a2 s .c o  m*/
 * 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.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;

import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.xml.sax.InputSource;

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 parseXMLValue(String xml, String xpathExpression)
            throws IOException, XPathExpressionException {
        try (InputStream is = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8.name()))) {
            XPath xpath = XPathFactory.newInstance().newXPath();
            InputSource inputSource = new InputSource(is);

            xpath.setNamespaceContext(ihcNamespaceContext);
            return (String) xpath.evaluate(xpathExpression, inputSource, XPathConstants.STRING);
        }
    }
}

Related

  1. isXPathAbsolute(String path)
  2. loadFromXmlNode(String xmlFile, String prefix)
  3. loadNodeList(String file, String path)
  4. merge(XPathExpression expression, InputStream... files)
  5. modelPathExpr()
  6. parseXPath(String expression, NamespaceContext nsContext)
  7. pathExists(XPath xpath, String expression, Object object)
  8. readXmlNodeChildFromFile(String xPath, String path, NamespaceContext nsContext)
  9. replaceXPathNode(Node replaceThis, Node replaceWith)