Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7571493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:41:17+00:00 2026-05-30T15:41:17+00:00

I posted this on the Jsoup group at google.groups, but there doesn’t seem to

  • 0

I posted this on the Jsoup group at google.groups, but there doesn’t seem to be much activity there lately, so I’ll try here as well…

The following code

final String html = "<html><head></head><body><div></div></body></html>";
Document doc = Jsoup.parse(html);
Element body = doc.body();
Element div = body.select("div").first();
body.empty();                   // <--- gives exception at line 56 below
// body.children().remove();    // does not give exception
body.appendChild(div);          // line 56, IndexOutOfBoundsException here

with Jsoup 1.6.1, gives me an IndexOutOfBoundsException with the following (partial, top 7 lines) stack trace

at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.remove(ArrayList.java:387)
at org.jsoup.nodes.Node.removeChild(Node.java:394)
at org.jsoup.nodes.Node.reparentChild(Node.java:420)
at org.jsoup.nodes.Node.addChildren(Node.java:402)
at org.jsoup.nodes.Element.appendChild(Element.java:225)
at webfilter.FilterY.<init>(FilterY.java:56)

here FilterY is my class containing the code above. If I use body.children().remove() instead of body.empty(), it works fine.

Question is… am I abusing Jsoup here, or is this really a bug?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:41:18+00:00Added an answer on May 30, 2026 at 3:41 pm

    Yes you are using the jSoup library in a wrong way. Lets go line by line:

    You are saving a reference of first child:

    Element div = body.select("div").first();
    

    You are removing all of the element’s child nodes:

    body.empty();
    

    And then you are trying to append the deleted child:

    body.appendChild(div);
    

    Now the problem is in the last step because when you deleted all the child elements then at that time the parent-child relationship is not broken because body.empty(); just calls the clear(); method of the java.util.List and it does not break any parent-child linkage, which is not the case when you do body.children().remove() and that’s why in the following code you are getting the mentioned exception as there are dangling reference to child node:

     protected void removeChild(Node out) {
            Validate.isTrue(out.parentNode == this);
            int index = out.siblingIndex();
            childNodes.remove(index); // problem comes here
            reindexChildren();
            out.parentNode = null;
        }
    

    But if you do something like this then it would work:

    Element div_child = doc.createElement("div");
    body.appendChild(div_child); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I posted this back in May on the [android-developers] Google Group. I never heard
[I have posted this at the Django users | Google Groups also.] Using the
I posted this question on the django-users list, but haven't had a reply there
I've posted this on Facebook developers forum, but things seem to move slowly over
I posted this on GAE for Java group, but I hope to get some
I posted this question on webmasters.stackexchange.com but no reply, so I thought I'd try
I posted this question here before but there were no responses. I may have
I posted this in the IIS forums but there has been no replies. So
Already posted this in Google Analytics help forum, but noone could help. Hopefully I
I posted this very same item on SERVERFAULT, but got no reply. So here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.