Wang Jun
Wang Jun

Reputation: 23

How can I get the LineNumber of the element when using Jsoup?

such as:

Document doc = Jsoup.parse(file,"UTF-8");

Elements eles = doc.getElementsByTag("style");

How can I get the lineNumber of eles[0] in the file?

Upvotes: 2

Views: 1015

Answers (3)

Ammar
Ammar

Reputation: 1158

This worked for me using v1.18.34. Putting it here because this was the first google result when I first started looking.

    Document doc = Jsoup.parse(html, Parser.htmlParser().setTrackPosition(true));

    doc.body().children()
            .stream().filter(element -> element.tagName().equals("p") || element.tagName().startsWith("h"))
            .forEach(element -> {
                int lineNumber = element.sourceRange().start().lineNumber();
                System.out.println(element.tagName() + " @ Line number: " + lineNumber);
            });

Upvotes: 1

There is no direct way. But there is an indirect way. Once you find the point of interest like an attribute, simply add a token as html before the element, and write the file to another temporary file. The next step is do a search for the token, using text editing tools.

code is as follows.

Step-1:

// get an element
for (Element element : doc.getAllElements()) {
... some code to get attributes of element ...

String myAttr = attribute.getKey();
if (myAttr.equals("some-attribute-name-of-interest") {
System.out.println(attribute.getKey() + "::" + attribute.getValue());
element.before("<!-- My Special Token : ABCDEFG -->");
}

Step-2:

// write the doc back to a temporary file
// see: How to save a jsoup document as text file

Step-3:

The last step is search for "My Special Token : ABCDEFG" in the output file using a text editing tool.

jsoup is a nice library. I thought this would help others.

Upvotes: 1

Aleh Maksimovich
Aleh Maksimovich

Reputation: 2650

There is no way for you to do it with Jsoup API. I have checked on their source code: org.jsoup.parser.Parser maintains no position information of the element in the original input.

Please, refer to sources on Grep Code

Provided that Jsoup is build for extracting and manipulating data I don't believe that they will have such feature in future as it is ambigous what element position is after manipulation and costly to maintain actual references.

Upvotes: 1

Related Questions