您可以使用XPath查詢,東西像/a/b/property[@name='driverClass']/text()
這將返回t的property
節點,其具有的屬性name
等於driverClass
的EXT值,這是一個子b
,這是a
例如兒童...
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = dbf.newDocumentBuilder().parse(JavaApplication244.class.getResourceAsStream("/Test.xml"));
XPathFactory xf = XPathFactory.newInstance();
XPath xPath = xf.newXPath();
XPathExpression expression = xPath.compile("https://stackoverflow.com/a/b/property[@name='driverClass']/text()");
String value = expression.evaluate(doc);
System.out.println(value);
} catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException exp) {
exp.printStackTrace();
}
其輸出oracle
查看w3schools, xPath和How XPath Works瞭解更多詳情
您是否想要所有具有attribute或ju的節點st屬性'節點? 「屬性」節點是否需要是'a'的孩子的'b'的孩子? – MadProgrammer
我只需要屬性節點值使用屬性值 – codeCrack
是的屬性節點需要b的孩子,這是一個 – codeCrack