我将一个xml文件转换为txt文件,只包含节点路径。 txt文件是这样的:
root/book/author/infos/#text = Empire Burlesque
root/book/title/#text = XML Developer's
root/book/genre/#text = Computer
root/book/price/#text = 44.95
root/book/publish_date/#text = 2000-10-01
root/book/description/#text = An in-depth look at creating applications
root/book/author/infos/#text = Emhhsjshhh
root/book/title/#text = Midnight Rain
root/book/genre/#text = Fantasy
root/book/price/#text = 5.95
root/book/publish_date/#text = 2000-12-16
root/book/description/#text = A former architect battles corporate zombies,
root/A1/A1A/#text = 1000
root/A1/A1B/#text = 2000
root/A1/A1C/#text = 3000
root/A2/A2A/#text = 4000
root/A2/A2B/#text = 5000
我想阅读上一个txt文件并得到另一个这样的结果,包括只有通信路径:
root/book/
root/A1/
root/A2/
这是我的代码java,它浏览xml文件并获取每个节点的路径:
public void get_full_path_nodes(Document doc) throws XPathExpressionException, IOException {
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/root/descendant::node()");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
// System.out.println(doc.getDocumentElement());
PrintWriter out = new PrintWriter(new FileWriter("C:\\Users\\dell AC\\Desktop\\dataset\\all_paths.txt"));
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
StringBuilder path = new StringBuilder(node.getNodeName());
String value = node.getTextContent();
node = node.getParentNode();
while (node.getNodeType() != Node.DOCUMENT_NODE) {
path.insert(0, node.getNodeName() + '/');
node = node.getParentNode();
}
out.println(path + " = " + value);
}
out.close();
} }