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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:24:21+00:00 2026-05-25T11:24:21+00:00

I have a Big html in String variable and I want to get contents

  • 0

I have a Big html in String variable and I want to get contents of a div. I can not rely on regular expression because it can have nested div’s. So, let’s suppose I have following String –

String test = "<div><div id=\"mainContent\">foo bar<div>good best better</div>  <div>test test</div></div><div>foo bar</div></div>";

Then how can I get this with a simple java program –

<div id="mainContent">foo bar<div>good best better</div>  <div>test test</div></div>

Well my approch is something like this (might be horrable, still fighting to correct) –

public static void main(String[] args) {
            int count = 1;
        int fl = 0;
        String s = "<div><div id=\"mainContent\">foo bar<div>good best better</div>  <div>test test</div></div><div>foo bar</div></div>";
        String tmp = s;
        int len = s.length();
        for (int i=0; i<len; i++){
            int st = s.indexOf("div>");
            if(st > -1) {
                char c = s.charAt(st-1);
                if(c == '/') {
                    count--; 
                } else {
                    count++;
                }
                s = s.substring(st+4);
                System.out.println(s);
                i = i + st;
                System.out.println(c + " -- " + st + " -- " + count + " -- " + i);  
                if (count == 0) {
                    fl = i;
                    break;
                }
            }
        }
        System.out.println("final ind - " + fl);
        s = tmp.substring(0, fl + 4);
        System.out.println("final String - " + s);
}
  • 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-25T11:24:22+00:00Added an answer on May 25, 2026 at 11:24 am

    I would recommend using JSoup to parse the HTML and find what you are looking for.

    It fulfills the simple requirement for sure. You can do what you want in just a couple of lines of code!

    jsoup is a Java library for working with real-world HTML. It provides
    a very convenient API for extracting and manipulating data, using the
    best of DOM, CSS, and jquery-like methods.

    jsoup implements the WHATWG HTML5 specification, and parses HTML to
    the same DOM as modern browsers do.

    scrape and parse HTML from a URL, file, or string

    find and extract data, using DOM traversal or CSS selectors

    jsoup is designed to deal with all varieties of HTML found in the
    wild; from pristine and validating, to invalid tag-soup; jsoup will
    create a sensible parse tree.

    Using the selector syntax makes finding and extracting data extremely simple.

    public static void main(final String[] args)
    {
        final String s = "<div><div id=\"mainContent\">foo bar<div>good best better</div>  <div>test test</div></div><div>foo bar</div></div>";
        final Document d = Jsoup.parse(s);
        final Elements e = d.select("#mainContent");
        System.out.println(e.get(0));
    }
    

    outputs

      <div id="mainContent">
       foo bar
       <div>
        good best better
       </div> 
       <div>
        test test
       </div>
      </div>
    

    Doesn’t get much more simple than that!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

something driver me crazy here i have a big HTML template which i can't
I have a big problem. I want to extract text from html table that
I have a string big_html and I want to add it to some div.
I have quite big document in html format that generated from Microsoft Word. It
I have a big chunk of HTML which is a template for some data
so I have this HTML table with a bunch of big numbers in it
I have a JLabel that needs to display some html-formatted text. However, I want
I have fooled around with regex but can't seem to get it to work.
I have a string of source code HTML. So I would go: int X
I have a big object that I resorted to serializing using @Html.Serialize() : [Serializable]

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.