[英]Get DIV contents with Java Swing
I'm trying to get DIV contents from previously fetched HTML document.我正在尝试从以前获取的 HTML 文档中获取 DIV 内容。 I'm using Java Swing.
我正在使用 Java Swing。
final java.io.Reader stringReader = new StringReader(html);
final HTMLEditorKit htmlKit = new HTMLEditorKit();
final HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
final HTMLEditorKit.Parser parser = new ParserDelegator();
parser.parse(stringReader, htmlDoc.getReader(0), true);
final javax.swing.text.Element el = htmlDoc.getElement("id");
This code should get a DIV with ID of "id" that I have inside html.这段代码应该得到一个ID为“id”的DIV,我在html中有。 But what next?
但是接下来呢? How to get the contents of div?
如何获取div的内容? Been searching it all around but only thing I found is how to get attribute value, not the Element contents.
一直在四处搜索,但我唯一发现的是如何获取属性值,而不是元素内容。
Should I move to jsoup?我应该搬到 jsoup 吗? I would rather use Java native, but so far I'm stuck.
我宁愿使用 Java 本机,但到目前为止我被卡住了。
Thanks!谢谢!
not the Element contents.
不是元素内容。
Try something like:尝试类似:
int start = el.getStartOffset();
int end = el.getEndOffset();
String text = htmlDoc.getText(start, end - start);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.