我做错了还是1.10.1中的错误

时间:2016-11-04 08:06:00

标签: jsoup

我有JSOUP v1.9.2并且一切正常,在v1.10.1中我得到“ConcurrentModificationException”(代码结尾)。我有两次关于HTML的这个问题。我首先从HTML设置中删除并设置一个。我做错了还是1.10.1中的错误?代码:

final Element typeElementObjaveSuda = document.getElementById( Type.OBJAVE_SUDA.getName() );
        if ( typeElementObjaveSuda != null ) {
                typeElementObjaveSuda.removeAttr("checked");
        }
        final Element typeElementObjaveJavnogBiljeznika = document.getElementById( Type.OBJAVE_JAVNOG_BILJEZNIKA.getName() );
        if ( typeElementObjaveJavnogBiljeznika != null ) {
                typeElementObjaveJavnogBiljeznika.removeAttr( "checked");
        }
        final Element typeElementCurrent = document.getElementById( tip.getName() );
        if ( typeElementCurrent != null ) {
            typeElementCurrent.attr( "checked", "checked" );
        }

引起:java.util.ConcurrentModificationException:null     在java.util.LinkedHashMap $ LinkedHashIterator.nextNode(LinkedHashMap.java:730)〜[na:1.8.0-internal]     在java.util.LinkedHashMap $ LinkedKeyIterator.next(LinkedHashMap.java:753)〜[na:1.8.0-internal]     at org.jsoup.nodes.Attributes.removeIgnoreCase(Attributes.java:122)~ [jsoup-1.10.1.jar:na]     at org.jsoup.nodes.Node.removeAttr(Node.java:128)~ [jsoup-1.10.1.jar:na]     在hr.ibm.oglasna.statistics.OglasnaCollector.fetchResult(OglasnaCollector.java:167)〜[classes /:na]     ...省略了12个常见帧

1 个答案:

答案 0 :(得分:0)

这似乎是一个错误:

String htmlString = " <input type=\"radio\" id=\"edit-rid-1\" name=\"rid\" value=\"1\" checked=\"checked\" class=\"form-radio\" />";

Element element = Jsoup.parse(htmlString).getElementById("edit-rid-1");

System.out.println(element.toString());
System.out.println(element.hasAttr("checked"));
element.removeAttr("checked");
System.out.println(element.toString());

使用jsoup 1.9.1输出

<input type="radio" id="edit-rid-1" name="rid" value="1" checked class="form-radio">
true
<input type="radio" id="edit-rid-1" name="rid" value="1" class="form-radio">

使用jsoup 1.10.1输出

<input type="radio" id="edit-rid-1" name="rid" value="1" checked class="form-radio">
true
Exception in thread "main"
java.util.ConcurrentModificationException
    at java.util.LinkedHashMap$LinkedHashIterator.nextNode(Unknown Source)
    at java.util.LinkedHashMap$LinkedKeyIterator.next(Unknown Source)
    at org.jsoup.nodes.Attributes.removeIgnoreCase(Attributes.java:122)
    at org.jsoup.nodes.Node.removeAttr(Node.java:128)
    at Webscraping.JsoupExamples.localByID(JsoupExamples.java:603)
    at Tester.App.main(App.java:119)

因此element.removeAttr("checked");会导致异常。在github上打开了一个问题:https://github.com/jhy/jsoup/issues/785

来自开发者的回复

  

嗨,谢谢你的报道。这与在HEAD

上修复的#759相同