2017-09-29 57 views
2

我用jsoup解析html頁面並提交表單。我需要在提交表單前刪除「返回」按鈕。我用element.remove()的方法,但後來我看到form.formData()沒有改變。已請求的元素已從form.children()中刪除,但存在於form.elements()中。這是一個錯誤,或者我用錯誤的方式從表單中刪除元素?JSoup:如何從窗體中刪除元素?

public class JsoupCheck { 
    public static void main(String[] args) { 
     String html = "<html><body><form action=\"demo\">" 
       + "<input type=\"submit\" name=\"buttonSave\" value=\"Save\">" 
       + "<input type=\"submit\" name=\"buttonBack\" value=\"Back\">" 
       + "<select name=\"selection\">" 
       + " <option value=\"value1\">Value 1</option>" 
       + " <option value=\"value2\" selected>Value 2</option>" 
       + " <option value=\"value3\">Value 3</option>" 
       + "</select>" 
       + "</form></body></html>"; 
     Document doc = Jsoup.parse(html); 
     FormElement form = (FormElement) doc.select("form").first(); 
     Element e = form.select("form").first(); 

     System.out.println("=== Original content of form"); 
     System.out.println(e); 
     System.out.println("=== Original content of form.formData()"); 
     for (Connection.KeyVal i : form.formData()) { 
      System.out.println(i.key() + "=" + i.value()); 
     } 
     System.out.println("form.elements().size() = " + form.elements().size()); 
     System.out.println("form.children().size() = " + form.children().size()); 

     e.select("input[name=buttonBack]").remove(); 
     System.out.println(); 

     System.out.println("=== Content of form after remove buttonBack (result: buttonBack removed)"); 
     System.out.println(e); 
     System.out.println("=== Content of form.formData() after remove buttonBack (result: buttonBack exist)"); 
     for (Connection.KeyVal i : form.formData()) { 
      System.out.println(i.key() + "=" + i.value()); 
     } 
     System.out.println("form.elements().size() = " + form.elements().size()); 
     System.out.println("form.children().size() = " + form.children().size()); 
    } 
} 

輸出是:

=== Original content of form 
<form action="demo"> 
<input type="submit" name="buttonSave" value="Save"> 
<input type="submit" name="buttonBack" value="Back"> 
<select name="selection"> <option value="value1">Value 1</option> <option value="value2" selected>Value 2</option> <option value="value3">Value 3</option></select> 
</form> 
=== Original content of form.formData() 
buttonSave=Save 
buttonBack=Back 
selection=value2 
form.elements().size() = 3 
form.children().size() = 3 

=== Content of form after remove buttonBack (result: buttonBack removed) 
<form action="demo"> 
<input type="submit" name="buttonSave" value="Save"> 
<select name="selection"> <option value="value1">Value 1</option> <option value="value2" selected>Value 2</option> <option value="value3">Value 3</option></select> 
</form> 
=== Content of form.formData() after remove buttonBack (result: buttonBack exist) 
buttonSave=Save 
buttonBack=Back 
selection=value2 
form.elements().size() = 3 
form.children().size() = 2 

回答

2

FormElement是一種特殊的節點。除了維護所有兒童(從Node繼承)的列表外,它還包含表格中所有元素的第二個內部列表。

public class FormElement extends Element { 
    private final Elements elements = new Elements(); 
    ... 
} 

當你調用Node#remove對孩子,它會更新兒童的父母的列表,而不是內部列表。

因此,要真正刪除元素,你還需要從這個內部列表中刪除:

e.select("input[name=buttonBack]").remove(); 
form.elements().removeIf(e -> e.attr("name").equals("buttonBack"));