TheNiceGuy
TheNiceGuy

Reputation: 3730

Jsoup get class name

I parse the elements with

 Elements input1 = pageListingParsed.select("form[name=MailForm] textarea");

Example output would be:

 <textarea name="dec13d35885064571998cc1c81facc28" rows="5" wrap="virtual" class="form-control c-545599b92f2d2b5a09f21c06d490e810"></textarea>

How can I get the class names? In that case I would need to assign the c-545599b92f2d2b5a09f21c06d490e810 to a variable.

Thanks

Upvotes: 1

Views: 5844

Answers (2)

luksch
luksch

Reputation: 11712

You can get all classes of an element with the JSoup method classNames. This is how you would use it:

Elements input1 = pageListingParsed.select("form[name=MailForm] textarea");
Set<String> classNames = input1.first().classNames();

Note that class names may not come in the same order in the HTML you parse. This is why the method returns a set and not an ordered data structure.

Upvotes: 0

nafas
nafas

Reputation: 5423

if you are sure the element size is 1, then you need to get the first Element and use method attr(...) for it:

Element e = input1.get(0);
System.out.println(e.attr("class"));

the output will be :

form-control c-545599b92f2d2b5a09f21c06d490e810

EDIT:

to only get the second part, you can simply use String.split(regex) method on it.

e.g.

String s = "form-control c-545599b92f2d2b5a09f21c06d490e810";
System.out.println(s.contain(" ")? s.split(" ")[1] : s);

OUTPUT:

c-545599b92f2d2b5a09f21c06d490e810

Upvotes: 1

Related Questions