简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM