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.
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 thetext()method.You can even concat all the selectors in one because
select(div[class=foo]).select(span[class=bar]).text()is equivalent toselect(div[class=foo] span[class=bar]).text()which can be simplified toselect(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.
This is using chaining:
Otherwise this is the same using reflection: