Java XPath "if"语句

标签 java xml if-statement xpath

当我在 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/

相关文章:

java - 是否有办法让 Rest 服务调用返回纯字符串?

java - FRONT_CAMERA 不支持 d 错误 : Video Chat Feature powered by Applozic

java - 整理/管理数据以在 Web 应用程序中填充 "live activity feeds"

xml - 文档中的多个 XML 声明是否为格式正确的 XML?

c# - 如何解析XML文件

javascript - 如何降低每次点击的不透明度

java - 如何使用 Spring RestTemplate 将 JSON 作为参数发送到 url 中?

c# - 使用 LINQ 搜索 XML 文档

mysql - SQL多重依赖UPDATE ON,什么最有效?

javascript - 使用 onchronometerticklistener 的 if 语句