How to get text which is not part of any element using jsoup?

混江龙づ霸主 提交于 2019-12-11 03:17:45

问题


How to get the text which is not part of any element?

<br><b>Price:</b> &nbsp; Rs. 24,900.00 &nbsp; <br>

Here, how can one get the text Rs.24,900.00. Is this possible using jsoup?


回答1:


I suppose there is a parent element so you should select that first and after just select the "b" like the following code. Basically just find the element in front of your text.

Document doc = Jsoup.parse( "<br><b>Price:</b> &nbsp; Rs. 24,900.00 &nbsp; <br>");
Element el = doc.select("b").first();
String text = ((TextNode) el.nextSibling()).text();

I used first because I knew from your example that there is only one "b" element. In case you have multiple prices you have to iterate over all elements instead of using first.

Jsoup stores text as nodes. So nextSibling will return a node (TextNode) that follows after the "b" element and contains text value: "  Rs. 24,900.00  "



来源:https://stackoverflow.com/questions/29251045/how-to-get-text-which-is-not-part-of-any-element-using-jsoup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!