Java XPath Select getValueByXPathAsInt(Document document, String xpath)

Here you can find the source of getValueByXPathAsInt(Document document, String xpath)

Description

Get a value by xpath and return as an int.

License

Open Source License

Parameter

Parameter Description
document xml document to apply xpath to.
xpath the xpath to get the value from.

Return

value contained at the xpath, or 0 if parsing fails.

Declaration

public static int getValueByXPathAsInt(Document document, String xpath) 

Method Source Code

//package com.java2s;
/*//from   w w  w .  j a v a2 s .c  o  m
 * Jinx is Copyright 2010 by Jeremy Brooks
 *
 * This file is part of Jinx.
 *
 * Jinx is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Jinx is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Jinx.  If not, see <http://www.gnu.org/licenses/>.
*/

import org.w3c.dom.Document;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

public class Main {
    private static XPathFactory xPathFactory;

    /**
     * Get a value by xpath and return as an int.
     *
     * @param document xml document to apply xpath to.
     * @param xpath the xpath to get the value from.
     * @return value contained at the xpath, or 0 if parsing fails.
     */
    public static int getValueByXPathAsInt(Document document, String xpath) {
        int x = 0;

        try {
            x = Integer.parseInt(getValueByXPath(document, xpath));
        } catch (Exception e) {
            // ignore, will return 0
        }
        return x;
    }

    /**
     * Get a value by xpath.
     *
     * This method will return an empty string if there are any errors, such
     * as an invalid xpath or an invalid document object.
     *
     * @param document xml document to apply xpath to.
     * @param xpath the xpath to get the value from.
     * @return value from the xpath, or an empty string.
     */
    public static String getValueByXPath(Document document, String xpath) {
        String value = "";
        try {
            value = getxPath().evaluate(xpath, document).trim();
        } catch (Exception e) {
            // ignore; will return empty string
        }

        return value;
    }

    /**
     * @return an XPath ready to use.
     */
    private static XPath getxPath() {
        return xPathFactory.newXPath();
    }
}

Related

  1. getValueByXPath(Document document, String xpath)
  2. getValueFromXPath(Document document, String xpathString)
  3. getXmlElements(Document inXml, String xpath)
  4. selectElements(Element element, String xpathExpression)
  5. selectNodeIterator(Node nContextNode, String sXPath)