Remotejon
Remotejon

Reputation: 35

Getting text from between Tags in XML using jSoup

Im trying to get the value of <report_id> from the following statement

<start_task_response status_text="OK, request submitted" status="202"><report_id>524291e2-bde8-4cd8-b48b-59a38f347ff2</report_id></start_task_response>

I have tried using the following code but it doesn't seem to want to work?

Any help would be gratefully received.

String taskid = Jsoup.parse(input).getAllElements().tagName(report_id);

or

String taskid = Jsoup.parse(input).getAllElements().attr("report_id");

Thanks

EDIT: Ok, as asked below; I will update with the response included from the below solution. Here is my (working) code now...

         Elements elms = Jsoup.parse(input).select("start_task_response report_id");
   for (Element e : elms){
   String taskid = e.text();
   jTextField6.setText(taskid);
   System.out.println(taskid);

This still does not return anything into String taskid. I have also tried modifying the

("start_task_response report_id") 

to

("report_id")

Any further help would be appreciated

Upvotes: 0

Views: 767

Answers (1)

user2340612
user2340612

Reputation: 10704

Try with:

Elements elems = JSoup.parse(...).select("start_task_response report_id");
for (Element e : elems) {
    String txt = e.text();
}

You can choose between text() and ownText() methods. The difference is the following:

For example, given HTML < p >Hello < b >there< /b > now!< /p >, p.ownText() returns "Hello now!", whereas p.text() returns "Hello there now!". Note that the text within the b element is not returned, as it is not a direct child of the p element

Upvotes: 1

Related Questions