Search code examples
javadomdom4j

What is wrong with the following java methods?


I am trying to translate the Javascript function here.

This will produce a cardinal xpath string given a org.w3c.dom.DOMElement: /html/body/p[3]/a

getElementXpath works fine if I comment out the getElementIdx() function and the conditional. 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.

What happens is that an empty string is returned! I tried printing out the path inside the getElementXpath function but to no avil. Expected output is a cardinal xpath string!

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???

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;
    }

Solution

  • The only reason for getting an empty String is that this expression evaluates to 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. 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. 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**? Please double check your actual code, if it is the same as the code you've pasted into the question