2017-04-19 55 views
0

這是我無法解決的問題。我有一個.xml文件,不是與父母和孩子,但與大孩子和多孫子。每個元素名稱都是唯一的,所以我需要一個方法來查找並返回可搜索元素。到目前爲止我的代碼:如何在循環中使用遞歸方法退出?

public static Element findElement(Element current, String nameOfElement) { 
      Element searchedElement = null; // this element I want to return 
      List children = current.getChildren(); // current is a root element passed to method 
      Iterator iterator = children.iterator(); 
      //label: 
      while (iterator.hasNext()) { 
       Element child = (Element)iterator.next(); 
       if (child.getName().equals(nameOfElement)) { 
        searchedElement = child; // on some level it founds it 
        System.out.println("!!!"); // it's just a marker to see that action happens 
        //break label; 
        break; 
       } 
       findElement(child, nameOfElement); 
      } 
      return searchedElement;  
     } 

正常情況下,如果執行條件週期應根據打破阻止,但這種情況不會發生。另外我有一個關於添加標籤的想法。 (它在代碼中有註釋),但它也不起作用。遞歸啓動並清除變量搜索元素的值。 我找到了一個解決方案,但它看起來很醜,我不知道上面添加的代碼不起作用。我的解決方案是添加而不是遞歸調用findElement(child,nameOfElement);此編號:

Element el = findElement(child, nameOfElement); 
      if (el != null && el.getName().toLowerCase().equals((nameOfElement).toLowerCase())) { 
       searchedElement = el; 
       return searchedElement; 
      } 

請幫忙找解!

+1

'findElement(兒童,nameOfElement);'你丟棄遞歸調用的返回值(你不要將它存儲在'searledElement'中)。 –

回答

1

嘗試的價值,

public static Element findElement(Element current, String nameOfElement) { 
     Element searchedElement = null; // this element I want to return 
     List children = current.getChildren(); // current is a root element passed to method 
     Iterator iterator = children.iterator(); 
     //label: 
     while (iterator.hasNext()) { 
      Element child = (Element)iterator.next(); 
      if (child.getName().equals(nameOfElement)) { 
       searchedElement = child; // on some level it founds it 
       System.out.println("!!!"); // it's just a marker to see that action happens 
       //break label; 
       break; 
      } 
      searchElement = findElement(child, nameOfElement); 
      if(searchElement != null) 
       return searchElement; 

     } 
     return searchedElement;  
    } 
+0

這應該工作,並有'equalsIgnoreCase'爲'等於' – janith1024

+0

感謝您的意見 –

0

你只需要返回遞歸調用

public static Element findElement(Element current, String nameOfElement) { 
     Element searchedElement = null; // this element I want to return 
     List children = current.getChildren(); // current is a root element passed to method 
     Iterator iterator = children.iterator(); 
     //label: 
     while (iterator.hasNext()) { 
      Element child = (Element)iterator.next(); 
      if (child.getName().equals(nameOfElement)) { 
       searchedElement = child; // on some level it founds it 
       System.out.println("!!!"); // it's just a marker to see that action happens 
       //break label; 
       break; 
      } 
      searchedElement = findElement(child, nameOfElement);//HERE !!! 
     } 
     return searchedElement;  
    } 
+0

已驗證。這不起作用。但無論如何感謝您花費您的時間 –