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 6553197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:33:11+00:00 2026-05-25T12:33:11+00:00

I’m trying to do something similar to: Jsoup: How to get all html between

  • 0

I’m trying to do something similar to: Jsoup: How to get all html between 2 header tags

However, it seems my code is avoiding plain text.
The site I’m parsing has code setup in such a way:

div class = "quoted-message"
     Response. Can contain images, text, etc.
div class = "quoted-message"
     Another response to another quoted message

Code Snippet used to handle the actual messages:

Element quote = msg.select(".quoted-message").first();
Boolean hasQuote = false;
Elements siblings = null;
siblings = quote.siblingElements();
createQuotePost(quote);
List<Element> elementsBetween = new ArrayList<Element>();
    for (int i = 1; i < siblings.size(); i++) {
        Element sibling = siblings.get(i);
        if (! "div.quoted-message".equals(sibling.tagName())) {
            elementsBetween.add(sibling);
            }

        else {
            Log.v("location", "Clear and Process");
            processElementsBetween(elementsBetween);
            elementsBetween.clear();
        }
    }
    if (! elementsBetween.isEmpty())
        processElementsBetween(elementsBetween);

This, however, does not seem to work as I want it to. The responses to the code do not have any special formatting to them (ie: sitting in a p tag). Using a bit of logging, I can see they aren’t getting put into Elements siblings.
Siblings seems to just include line breaks and such.

Note: I’ve only tested this on small posts (simple one liners) to save on sifting through long pages of printouts.

Any suggestions on what to do?

EDIT:
Here is the HTML code snippet between 2 quoted-message divs:

    MESSAGE TO BE QUOTED
    </div>
    <br />
    <br />
    Hello quoted message
    <br />
    I am a response
    <br />
    <br />
    <div class="quoted-message">
  • 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-25T12:33:12+00:00Added an answer on May 25, 2026 at 12:33 pm

    Think one of the problems is you’re asking for Elements and not Nodes. Text nodes are Nodes and not Elements.

    Try this:

    package grimbo.test;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.nodes.Node;
    import org.jsoup.select.Elements;
    
    public class StackOverflow {
        public static void main(String[] args) {
            String html = "<div class=quoted-message>message-1</div>\n    <br />\n    <br />\n    Hello quoted message\n    <br />\n    I am a response\n    <br />\n    <br />\n";
            html += "<div class=quoted-message>message-2</div>\n    <br />\n    <br />\n    Hello quoted message\n    <br />\n    I am a response\n    <br />\n    <br />\n";
            Document doc = Jsoup.parse(html);
            handleQuotedMessages(doc.select(".quoted-message"));
        }
    
        private static void handleQuotedMessages(Elements quotedMessages) {
            Element firstQuotedMessage = quotedMessages.first();
            List<Node> siblings = firstQuotedMessage.siblingNodes();
            List<Node> elementsBetween = new ArrayList<Node>();
            Element currentQuotedMessage = firstQuotedMessage;
            for (int i = 1; i < siblings.size(); i++) {
                Node sibling = siblings.get(i);
    
                // see if this Node is a quoted message
                if (!isQuotedMessage(sibling)) {
                    elementsBetween.add(sibling);
                } else {
                    createQuotePost(currentQuotedMessage, elementsBetween);
                    currentQuotedMessage = (Element) sibling;
                    elementsBetween.clear();
                }
            }
            if (!elementsBetween.isEmpty()) {
                createQuotePost(currentQuotedMessage, elementsBetween);
            }
        }
    
        private static boolean isQuotedMessage(Node node) {
            if (node instanceof Element) {
                Element el = (Element) node;
                return "div".equals(el.tagName()) && el.hasClass("quoted-message");
            }
            return false;
        }
    
        private static List<Element> filterElements(String tagName, List<Node> nodes) {
            List<Element> els = new ArrayList<Element>();
            for (Iterator<Node> it = nodes.iterator(); it.hasNext();) {
                Node n = it.next();
                if (n instanceof Element) {
                    Element el = (Element) n;
                    if (el.tagName().equals(tagName)) {
                        els.add(el);
                    }
                }
            }
            return els;
        }
    
        private static void createQuotePost(Element quote, List<Node> elementsBetween) {
            System.out.println("createQuotePost: " + quote);
            System.out.println("createQuotePost: " + elementsBetween);
            List<Element> imgs = filterElements("img", elementsBetween);
            // handle imgs
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a text area in my form which accepts all possible characters from

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.