Java XPath Expression XPathValueFromString(String sIn, String sxpath)

Here you can find the source of XPathValueFromString(String sIn, String sxpath)

Description

Returns the xml value.

License

Open Source License

Parameter

Parameter Description
sIn a parameter
sxpath a parameter

Exception

Parameter Description
ParserConfigurationException an exception
IOException an exception
SAXException an exception
XPathExpressionException an exception

Declaration

public static String XPathValueFromString(String sIn, String sxpath)
        throws ParserConfigurationException, IOException, SAXException,
        XPathExpressionException 

Method Source Code

//package com.java2s;
/**//from  w w  w  .  j  av  a2 s  . c  om
 * Copyright (c) 2010-2016, openHAB.org and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    /**
     * Returns the xml value.
     * 
     * @param sIn
     * @param sxpath
     * @return
     * @throws ParserConfigurationException
     * @throws IOException
     * @throws SAXException
     * @throws XPathExpressionException
     */
    public static String XPathValueFromString(String sIn, String sxpath)
            throws ParserConfigurationException, IOException, SAXException,
            XPathExpressionException {
        // DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        Document doc = loadXMLFromString(sIn);
        XPath xPath = XPathFactory.newInstance().newXPath();
        // XPath Query for showing all nodes value
        XPathExpression expr = xPath.compile(sxpath);

        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        String sReturn = "";
        for (int i = 0; i < nodes.getLength(); i++) {
            sReturn = nodes.item(i).getNodeValue();
        }
        return sReturn;
    }

    /**
     * Loads a string into a xml document object.
     * 
     * @param xml
     * @return
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public static Document loadXMLFromString(String xml)
            throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(xml));
        Document doc = builder.parse(is);
        return doc;
    }
}

Related

  1. string(String fileName, String xpathExpression)
  2. valueOf(Node node, String xpath, NamespaceContext context)
  3. xmlValueOf(Node node, String xpathExpression)
  4. xPathStr(String expr, Object context)
  5. xpathToNode(String xpathQuery, Object domObject)