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

  • Home
  • SEARCH
  • 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 8466081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:14:15+00:00 2026-06-10T15:14:15+00:00

I want to call a method in jsoup dynamically. Here is my use case,

  • 0

I want to call a method in jsoup dynamically. Here is my use case,
I am actually calling multiple select dom methods in jsoup to traverse interior like,

Document doc = Jsoup.connect("http://test.com").get();
String companyName = doc.select("div[class=\"_name\"]").select("span[class="\_name\"]").text();

Can I achieve the same dynamically like,

Document doc = Jsoup.connect("http://test.com").get();
String pattern = "select("div[class=\"_name\"]").select("span[class="\_name\"]").text()";
String companyName = doc.pattern;

I know we can use reflection to achieve but not sure how to implement this behaviour as pattern string will be dynamic and may have n number of dom selectors.

Let me know if the above dynamism is possible.

  • 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-06-10T15:14:17+00:00Added an answer on June 10, 2026 at 3:14 pm

    Yes it is possible, all you have to do is call repetitively the select() method on the Document object until you have enumerated all the selectors, and the call the text() method.

    You can even concat all the selectors in one because select(div[class=foo]).select(span[class=bar]).text() is equivalent to select(div[class=foo] span[class=bar]).text() which can be simplified to select(div.foo span.bar).text()

    So maybe you can even discard the whole reflexion thing and create dynamically a correct and direct selector to what you want.

      Document doc = Jsoup.connect("http://test.com").get();
      String companyName = doc.select("div.foo span.bar").text();
    

    This is using chaining:

    Document doc = Jsoup.connect("http://test.com").get();
    List<String> criterias = Arrays.asList("div.foo", "span.bar");
    Document tmpDoc = doc;
    for (String criteria: criterias) {
        if (tmpDoc != null)
            tmpDoc = tmpDoc.select(criteria);
    }
    // now you have traversed the whole criterias just get the text
    String companyName = tmpDoc.text();
    

    Otherwise this is the same using reflection:

    Document doc = Jsoup.connect("http://test.com").get();
    List<String> criterias = Arrays.asList("div.foo", "span.bar");
    Method select = doc.getClass().getMethod("select", String.class);
    Document tmpDoc = doc;
    for (String criteria: criterias) {
        if (tmpDoc != null)
            tmpDoc = (Document)select.invoke(tmpDoc, new Object[] {criteria});
    }
    // now you have traversed the whole criterias just get the text
    String companyName = tmpDoc.text();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call a method that returns a string value. Actually this string
Hi I want to call a method based on one time span here is
I want to dynamically call the method of a custom class much like the
here is the situation: I want to call a method from a C++ module,
I have a requirement: I want to call method every day at any particular
I want to call a method say success(message) of javascript from android activity. I
I want to call a method of my class inside a lambda expression: void
I want to call a method, pass it the length and have it generate
i want to call a method that placed in tag in master page like
I'm using CoreLocation,and I want to call a method to do some computations on

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.