Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// under the terms of the GNU General Public License as published by the

import org.w3c.dom.*;

import javax.xml.xpath.*;
import java.util.*;

public class Main {
    public static List<Element> SelectElements(Element parent, String expression) {
        List<Element> nodes = new ArrayList<Element>();
        XPath xpath = getXPath();
        try {
            XPathExpression xpathExpression = xpath.compile(expression);
            NodeList nodeList = (NodeList) xpathExpression.evaluate(parent, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                nodes.add((Element) (nodeList.item(i)));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return nodes;
    }

    public static XPath getXPath() {
        return getXPathFactory().newXPath();
    }

    public static XPathFactory getXPathFactory() {
        return XPathFactory.newInstance();
    }
}