首页javaxpathJava HTML/XML - 如何通过键检索XML中的数据

Java HTML/XML - 如何通过键检索XML中的数据

我们想知道如何通过键检索XML中的数据。
import java.io.StringReader;

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

import org.xml.sax.InputSource;

public class Main {

  private static String XML = "<APP START='2014-08-25 13:45:58' "
      + "SINCE='2016-08-25 13:45:58' " + "STATE='Running' " + "NAME='abc' "
      + "ID='4305' " + "Codebase='www.w3cschool.cn' " + "Build-Revision='123' "
      + "Build-Label='128.3'></APP>";

  public static void main(String[] args) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource xml = new InputSource(new StringReader(XML));
    String result = (String) xpath.evaluate("//@Build-Label", xml,
        XPathConstants.STRING);
    System.out.println(result);
  }

}