Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;
import java.util.List;

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

import org.w3c.dom.Element;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static List<Element> getXPathList(Node node, String expr) {
        assertNotNull("node == null", node);
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        List<Element> result = new ArrayList<Element>();
        try {
            NodeList nl = (NodeList) xpath.evaluate(expr, node, XPathConstants.NODESET);
            for (int i = 0; i < nl.getLength(); i++) {
                result.add((Element) nl.item(i));
            }
            return result;
        } catch (XPathExpressionException e) {
            throw new RuntimeException(e);
        }
    }

    private static void assertNotNull(String msg, Object o) {
        if (o == null) {
            throw new IllegalStateException(msg);
        }
    }
}