XPathFactory xpf = XPathFactory.newInstance();
XPath xp = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/descendant::title");
descendant
は任意の子孫ノードを表わす軸
::
は軸とノードテストを分離する軸分離符
title
は要素名がtitleであるノードを指定するノードテスト
NodeList nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
※ doc
は Document
クラスのインスタンス
List<String> titleList = new ArrayList<>();
for (int i = 0; i < nodeList.getLength(); i++) {
titleList.add(nodeList.item(i).getTextContent());
}
あとはお気の召すままに titleList
を料理してください。