Java HTML/XML - 如何在java中查找XML文件中特定屬性的值
我們想知道如何在java中查找XML文件中特定屬性的值。
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
public class Main {
public static void main(String[] args) throws Exception {
XPath xpath = XPathFactory.newInstance().newXPath();
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse("file:////tmp/whatever.xml");
String version = xpath.evaluate("http://behavior/@version", doc);
System.out.println(version);
}
}