如果您想在 XML 文件中搜索某些内容,您应该使用 XPath 。
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder builder = documentBuilderFactory
.newDocumentBuilder();
Document doc = builder.parse("path/to/xml/MyXML.xml");
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression expression = xpath
.compile("//titles");
NodeList nodes = (NodeList) expression.evaluate(doc,
XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
//System.out.println(nodes.item(i).getNodeName());
System.out.println(nodes.item(i).getTextContent());
}
} catch (Exception exception) {
exception.printStackTrace();
}
编辑
String input = "XMLAsString";
InputStream is= new ByteArrayInputStream(input.getBytes());
Document doc = builder.parse(is);