Java 怎么将xpath转为节点列表

2019年8月7日21:02:37Java 怎么将xpath转为节点列表已关闭评论 413 545字阅读1分49秒

1.简介

Java 怎么将xpath转为节点列表?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



import java.util.*;

import javax.xml.xpath.*;
import org.w3c.dom.*;

public class Main {
    public static List<Node> xpathToNodeList(String xpathQuery,
            Object domObject) throws XPathExpressionException {
        List<Node> result = new ArrayList<Node>();

        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        NodeList nodeList = (NodeList) xpath.evaluate(xpathQuery,
                domObject, XPathConstants.NODESET);

        for (int i = 0; i < nodeList.getLength(); i++)
            result.add(nodeList.item(i));

        return result;
    }
}

 
编程十万个怎么办