简体   繁体   English

以下java方法有什么问题?

[英]What is wrong with the following java methods?

I am trying to translate the Javascript function here. 我正在尝试翻译Javascript函数

This will produce a cardinal xpath string given a org.w3c.dom.DOMElement: /html/body/p[3]/a 给定org.w3c.dom.DOMElement会产生一个基本的xpath字符串: /html/body/p[3]/a

getElementXpath works fine if I comment out the getElementIdx() function and the conditional. 如果我注释掉getElementIdx()函数和条件函数,则getElementXpath可以正常工作。 Problem seems to be located in getElementIdx() , but I don't see what I am doing wrong, the Java code below is nearly same as the Javascript version. 问题似乎位于getElementIdx() ,但我看不出我在做什么错,下面的Java代码与Javascript版本几乎相同。

What happens is that an empty string is returned! 发生的情况是返回了空字符串! I tried printing out the path inside the getElementXpath function but to no avil. 我尝试打印出getElementXpath函数内部的路径,但没有打印出来。 Expected output is a cardinal xpath string! 预期的输出是基本的xpath字符串!

Update: 更新:

public static String getElementXpath(DOMElement elt){
        String path = ""; 

        for (; elt != null && elt.ELEMENT_NODE == elt.getNodeType(); elt = (DOMElement) elt.getParentNode()){

            path = "test";
            System.out.println(path); //this prints out fine.
        }
        System.out.println(path); //nothing is printed!
            return path;                            
    }

Why isn't path being printed, outside of the for loop??? 为什么在for循环之外不打印路径???

public static void main(String[] args){
    DOMDocument domDocument = (DOMDocument) browser.getDocument();
                        DOMElement currentElement = (DOMElement) domDocument.getElementFromId("uniqueLink");                      
                                System.out.println(getElementXpath(currentElement));

}

public static String getElementXpath(DOMElement elt){
    String path = ""; 

    for (; elt != null && elt.ELEMENT_NODE == elt.getNodeType(); elt = (DOMElement) elt.getParentNode()){
        int idx = getElementIdx(elt);
        String xname = elt.getTagName();

        if (idx > 1) xname += "[" + idx + "]";
        path = "/" + xname + path;  

    }
    System.out.println(path);
        return path;                            
}

public static int getElementIdx(DOMElement elt) {
        int count = 1;

         for (DOMElement sib = (DOMElement) elt.getPreviousSibling(); sib != null; sib = (DOMElement) sib.getPreviousSibling())
            {
                if(sib.ELEMENT_NODE == sib.getNodeType() && sib.getTagName().equals(elt.getTagName())){

                    count++;
                }
            }       
        return count;
    }

The only reason for getting an empty String is that this expression evaluates to false: 获取空字符串的唯一原因是此表达式的计算结果为false:

elt != null && elt.ELEMENT_NODE == elt.getNodeType()

So either the element you pass to the method is actually null or the elements node type is not identical with elt.ELEMENT_NODE . 因此,您传递给该方法的元素实际上为null或elements节点类型与elt.ELEMENT_NODE不相同。 In all other cases, the result would be / at least. 在所有其他情况下,结果至少为/

But because your passing an instance of DOMElement , the node should always be of ELEMENT_NODE type, so I pretty sure, that domDocument.getElementFromId("uniqueLink") returns null for you current document. 但是,因为您传递的是DOMElement实例,所以该节点应始终为ELEMENT_NODE类型,因此我可以肯定, domDocument.getElementFromId("uniqueLink")为您当前的文档返回null I'd check this first. 我先检查一下。


For clarification 为了澄清

public static String getElementXpath(DOMElement elt){
    String path = ""; 
    try {
      for (; elt != null && elt.ELEMENT_NODE == elt.getNodeType(); elt = (DOMElement) elt.getParentNode()){
        int idx = getElementIdx(elt);
        String xname = elt.getTagName();    
        if (idx > 1) {
           xname += "[" + idx + "]";
        }
        path = "/" + xname + path;  
        System.out.println("Inside: " + path);  // **1**
      }
    } catch(Exception e) {
        // unhides any exception thrown inside the for loop
        e.printStackTrace();   
    } finally {
        // forces a final print of "path", even if a runtime exception was raised
        System.out.println("Outside: " + path); // **2**
    }
    return path;                            
}

In this snippet, you see non-empty paths at **1** and empty paths at **2** ? 在这个片段中,你看到在非空路径**1** ,在和**2** Please double check your actual code, if it is the same as the code you've pasted into the question 请仔细检查您的实际代码,如果它与您粘贴到问题中的代码相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM