[英]How can I get the LineNumber of the element when using Jsoup?
如:
Document doc = Jsoup.parse(file,"UTF-8");
Elements eles = doc.getElementsByTag("style");
如何獲取文件中eles[0]
的lineNumber?
沒有直接的方法。 但是有一種間接的方式。 找到興趣點(如屬性)后,只需在元素之前添加標記(如html),然后將文件寫入另一個臨時文件即可。 下一步是使用文本編輯工具搜索令牌。
代碼如下。
//獲取一個元素
對於(Element element:doc.getAllElements()){
...一些代碼來獲取元素的屬性...
字符串myAttr = attribute.getKey();
如果(myAttr.equals(“ 感興趣的某些屬性名稱 ”){
System.out.println(attribute.getKey()+“ ::” + attribute.getValue());
element.before(“ <!-我的特殊令牌:ABCDEFG->”);
}
//將文檔寫回到臨時文件
//請參閱: 如何將jsoup文檔另存為文本文件
最后一步是使用文本編輯工具在輸出文件中搜索“我的特殊令牌:ABCDEFG”。
jsoup是一個不錯的庫。 我以為這會幫助別人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.