Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

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

public class Main {
    public static String getFirstValueFromXPath(Element parent, String xpath) throws Exception {
        try {
            XPath xPath = XPathFactory.newInstance().newXPath();
            return (String) xPath.compile(xpath).evaluate(parent, XPathConstants.STRING);
        } catch (XPathExpressionException xpee) {
            throw new Exception(xpee.getMessage());
        }
    }

    public static String getFirstValueFromXPath(Document parent, String xpath) throws Exception {
        try {
            XPath xPath = XPathFactory.newInstance().newXPath();
            return (String) xPath.compile(xpath).evaluate(parent, XPathConstants.STRING);
        } catch (XPathExpressionException xpee) {
            throw new Exception(xpee.getMessage());
        }
    }
}