Search code examples
javaintellij-ideajsoup

IntelliJ warning about filtering Jsoup Elements


I'm using Jsoup to parse HTML files and I want to get all the script elements whose type attribute is equal to "dingdong".

Here's the code I've done to try to accomplish that:

Stream<String> codeLines = document.getElementsByTag("script")
                .filter(element -> element.attr("type").contentEquals("dingdong"))
                .forEach(Element::data)
                .stream();

However, IntelliJ highlights attr in red and shows a warning:

enter image description here

I really can't see what the issue is that IntelliJ is making a fuss about, so any help would be much appreciated!

Thank you in advance!

Note: I'm using IntelliJ with JDK11.0.6.


Edit - Here are my imports, as asked:

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

import java.io.File;
import java.io.IOException;
import java.util.stream.Stream;

Solution

  • The .filter method expects a NodeFilter, not a Predicate as you may have assumed.

    You can achieve the same by using .select:

    Stream<String> codeLines = document.getElementsByTag("script")
                    .select("[type=dingdong]")
                    .stream()
                    .map(Element::data);