I have some problems when I use If Sentence inside a For to delete some nodes in a XML document, because I don't understand why my counters become to 0 after put the If Sentence:
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document document = dbf.newDocumentBuilder().parse(new File("input3.xml"));
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
XPathExpression expres = xpath.compile("//TNMS/TPaths/TPath[Topology/LayerSet/NonTerminatedLayers/Layer='RS64']");
NodeList PathsCount = (NodeList) expres.evaluate(document, XPathConstants.NODESET);
System.out.println("Inicia el Borrado:");
for (int i = 0; i < PathsCount.getLength(); i++)
{
System.out.println(i);
XPathExpression exprueba = xpath.compile("/TNMS/TPaths/TPath[1]/Topology/LayerSet/TerminatedLayers");
NodeList Count = (NodeList) exprueba.evaluate(document, XPathConstants.NODESET);
System.out.println("Hay " + Count.getLength());
int countvar = Count.getLength();
XPathExpression expression = xpath.compile("//TNMS/TPaths/TPath[Topology/LayerSet/NonTerminatedLayers/Layer='RS64']");
Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE);
b13Node.getParentNode().removeChild(b13Node);
}
I see next in the output:
Inicia el Borrado:
0
Hay 0
1
Hay 1
2
Hay 1
3
Hay 0
<?xml version="1.0" encoding="UTF-8" standalone="no"?><TNMS>
<TPaths>
</TPaths>
</TNMS>BUILD SUCCESSFUL (total time: 0 seconds)
But when i agree the If sentence:
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document document = dbf.newDocumentBuilder().parse(new File("input3.xml"));
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
XPathExpression expres = xpath.compile("//TNMS/TPaths/TPath[Topology/LayerSet/NonTerminatedLayers/Layer='RS64']");
NodeList PathsCount = (NodeList) expres.evaluate(document, XPathConstants.NODESET);
System.out.println("Inicia el Borrado:");
for (int i = 0; i < PathsCount.getLength(); i++)
{
System.out.println(i);
XPathExpression exprueba = xpath.compile("/TNMS/TPaths/TPath[1]/Topology/LayerSet/TerminatedLayers");
NodeList Count = (NodeList) exprueba.evaluate(document, XPathConstants.NODESET);
System.out.println("Hay " + Count.getLength());
int countvar = Count.getLength();
if (countvar == 1){
XPathExpression expression = xpath.compile("//TNMS/TPaths/TPath[Topology/LayerSet/NonTerminatedLayers/Layer='RS64']");
Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE);
b13Node.getParentNode().removeChild(b13Node);
}
}
the output is the next:
Inicia el Borrado:
0
Hay 0
1
Hay 0
2
Hay 0
3
Hay 0
and the erase is failed.
Any ideas?
Aucun commentaire:
Enregistrer un commentaire