当我在 for
循环中使用 if
语句删除 XML 文档中的某些节点时,我遇到了一些问题,因为我不明白为什么在放入 if
语句后我的计数器变为 0:
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);
}
我在输出中看到下一个:
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)
但是当我同意 if
语句时:
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);
}
}
输出如下:
Inicia el Borrado:
0
Hay 0
1
Hay 0
2
Hay 0
3
Hay 0
并且删除失败。
有什么想法吗?
最佳答案
您需要在 for 循环内相对于迭代的节点评估 xpath 表达式。在您的代码中,您不断引用 TPath[1]
这会导致各种奇怪的问题。
评估相对于您存储在 NodeList
中的当前 TPaths
节点的 xpath 表达式。
for (int i = 0; i < PathsCount.getLength(); i++)
{
Node tPathsChild = PathsCount.item(i);
System.out.println(i);
// get the number of TerminatedLayers elements.
XPathExpression exprueba = xpath.compile("Topology/LayerSet/TerminatedLayers");
NodeList Count = (NodeList) exprueba.evaluate(tPathsChild, XPathConstants.NODESET);
System.out.println("Hay " + Count.getLength());
int countvar = Count.getLength();
// remove this child if there are only 1 of them.
if (countvar == 1) {
tPathsChild.getParentNode().removeChild(tPathsChild);
}
}
仅供引用:Java 命名约定为 camelCase
。对于 Java 程序员来说,如果不是这种格式,则可能很难阅读。
关于Java XPath "if"语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32363794/