Так что мне нужно разобрать XML-файл с помощью XPath. Все работает хорошо, но если у меня есть тег, который присутствует более одного раза, мой код отображает все подталки всех тегов внутри тега. Например: у меня естьXPath Parser java redundancy
<a>
<b></b>
</a>
<c></c>
<a>
<b></b>
</a>
выход будет:
a
b
b
c
a
b
b
мой код это:
public void parseFileXPath() throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(file);
doc.getDocumentElement().normalize();
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
String expression;
Node node;
NodeList nodeList;
expression="/*";
node = (Node) xpath.evaluate(expression,doc, XPathConstants.NODE);
//System.out.println(node.getNodeName());
expression="/frogans-fsdl/*";
nodeList = (NodeList) xpath.evaluate(expression,doc, XPathConstants.NODESET);
NodeList nodeList1;
for (int i = 0; i < nodeList.getLength(); i++) {
String aTag = nodeList.item(i).getNodeName();
System.out.println(aTag);
expression="/frogans-fsdl/"+aTag+"/*";
nodeList1 = (NodeList) xpath.evaluate(expression,doc,XPathConstants.NODESET);
for (int j = 0; j < nodeList1.getLength(); j++) {
System.out.println("\t"+nodeList1.item(j).getNodeName());
//TODO
//Display what's inside each tag
}
expression="";
}
}
Любая помощь, пожалуйста?
Спасибо вам большое! –