首页javaxpathJava HTML/XML - 如何从XPath查询获取数字

Java HTML/XML - 如何从XPath查询获取数字

我们想知道如何从XPath查询获取数字。
import java.io.FileReader;

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

import org.xml.sax.InputSource;

public class Main {

    public static void main(String[] args) throws Exception {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();

        Number shows = (Number) xPath.evaluate("count(/schedule/show)",
                new InputSource(new FileReader("tds.xml")),
                XPathConstants.NUMBER);
        System.out.println("Document has " + shows.intValue() + " shows.");
    }

}