首页javaxpathJava HTML/XML - 如何获取嵌套元素

Java HTML/XML - 如何获取嵌套元素

我们想知道如何获取嵌套元素。
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 xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();

    InputSource xml = new InputSource("input.xml");
    String health = (String) xpath.evaluate("/Game/health", xml,
        XPathConstants.STRING);
    System.out.println(health);
  }

}