21

I have a problem similar to those:

  1. jQuery: exclude children from .text()

Is it possible to achive it in JSoup?

1 Answer 1

30

You are probably looking for calling ownText:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class Main {
    public static void main(String[] args) throws Exception {
        final Document document = Jsoup.parse("<html><head/><body><a href=\"#\" class=\"artist\">Soulive<span class=\"create-play\">Play</span></a></body></html>");
        final Element elem = document.getElementsByAttributeValue("class", "artist").first();
        System.out.println(elem.ownText());
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.