请注意,这个问题不是关于实现,而是关于编程技巧。
我正在尝试阅读一些HTML代码,然后创建一个对象/多个对象,以便再次将其绘制成格式化。
例如。想象一下这个html:
<body>
Hello, this is some plain and I'm going to attach an image.
<img src="someimage.jpg" />
And after the image I keep writting.
And as this is a forum message, you can add a div to quote like the following:
<div class="post-quote"> Some user said something</div>
And that was it!
</body>
如您所见,有几个元素,例如<img>
和<div>
。
我的总体目标是将所有内容分开:
然后,特定编程,它可能是List
的{{1}}。
通过此列表,我可以将这些元素绘制回到自定义格式化和定位的屏幕中。
但是,我无法找到如何使用某种逻辑方法划分HTML字符串。
你们有什么提示吗?您如何拆分此字符串以实现之前解释的问题?
谢谢!
欢迎提出问题!
修改 JSOUP是一个解析器。我不是在寻找解析器。我正在寻找关于如何保持已解析元素的顺序的提示。请重读我的问题!
答案 0 :(得分:0)
您应该使用HTML解析器,例如jsoup。
HTML上的示例:
Document doc = Jsoup.parse(html);
print(doc.select("img").attr("src")); ==> someimage.jpg
print(doc.select("div.post-quote").text()); ==> Some user said something