Java XPath Get getValueFromXML(final String inputXML, final String xPathQuery, final int index)

Here you can find the source of getValueFromXML(final String inputXML, final String xPathQuery, final int index)

Description

get Value From XML

License

Open Source License

Declaration

public static final String getValueFromXML(final String inputXML, final String xPathQuery, final int index)
            throws Exception 

Method Source Code


//package com.java2s;
/*//from w w w.ja v a  2 s. c  o  m
 * MIT License
 * 
 * Copyright (c) 2016 Amit Dixit (github.com/inbravo)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;

import org.w3c.dom.NodeList;

public class Main {
    public static final String getValueFromXML(final String inputXML, final String xPathQuery) throws Exception {

        /* Call another method */
        return getValueFromXML(inputXML, xPathQuery, 0);
    }

    public static final String getValueFromXML(final String inputXML, final String xPathQuery, final int index)
            throws Exception {

        /* Call another method */
        return getValueFromXML(inputXML, xPathQuery, index, null);
    }

    private static final String getValueFromXML(final String inputXML, final String xPathQuery, final int index,
            final String[] namespaces) throws Exception {

        /* new document builder factory */
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        /* Factory is name space aware */
        factory.setNamespaceAware(true);

        final DocumentBuilder builder = factory.newDocumentBuilder();
        final Document document = builder.parse(new ByteArrayInputStream(inputXML.getBytes()));

        /* Create new XPath object to query XML document */
        final XPath xpath = XPathFactory.newInstance().newXPath();

        /* XPath Query for showing all nodes value */
        final XPathExpression expr = xpath.compile(xPathQuery);

        if (index > 0) {

            /* Get node list from response document */
            final NodeList nodeList = (NodeList) expr.evaluate(document, XPathConstants.NODESET);

            /* Check if records founds */
            if (nodeList != null && nodeList.getLength() >= 0) {

                return nodeList.item(index).getTextContent();
            }

        } else {
            /* Get node list from response document */
            return expr.evaluate(document);
        }

        return null;
    }
}

Related

  1. getTextNodes(Node contextNode, String xPath)
  2. getValidXpath(String xPath, NamespaceContext context)
  3. getValue(final String expression, final String xml)
  4. getValueByPath(Node node, String path)
  5. getValueByXpath(Node doc, String xquery)
  6. getValues(final String expression, final String xml)
  7. getX_PATH()
  8. xGetNode(Node targetNode, String expression)