所以我下面有一个类似这样的html字符串:
<div class="row xyz">
<!--Multiple Other div's and tags-->
</div>
<div class="row xws">
<!--Multiple Other div's and tags-->
</div>
<div class="row daze">
<!--Multiple Other div's and tags-->
</div>
如您所见,它具有 3个父div !
如何用 JAVA 将HTML字符串除以父div的数量,而不使用动态生成的类名“ row xyz”,在这种情况下为3 所以我会得到:
字符串div1
字符串div2
字符串div3
其中div1 =
<div class="row xyz">
<!--Multiple Other div's and tags-->
</div>
AND
div2 =
<div class="row xws">
<!--Multiple Other div's and tags-->
</div>
AND
div3 =
<div class="row daze">
<!--Multiple Other div's and tags-->
</div>
答案 0 :(得分:0)
尝试使用jsoup,如下所示:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class MyClass {
public static void main(String args[]) {
String html = "<html><head/><body>"+
"<div class=\"row xyz\"> <div>div1</div> <div>div1_1</div> </div>"+
"<div class=\"row xws\"> <div>div2</div> </div>" +
"<div class=\"row daze\"> <div>div3</div></div>"+
"</body></html>";
Document document = Jsoup.parse(html);
Elements divs = document.select("body > div");//get first level of divs
for (int i=0; i< divs.size(); i++) {
System.out.println(String.format("div%d = %s", i + 1, divs.get(i).outerHtml()));
}
}
}
输出将是:
div1 = <div class="row xyz">
<div>
div1
</div>
<div>
div1_1
</div>
</div>
div2 = <div class="row xws">
<div>
div2
</div>
</div>
div3 = <div class="row daze">
<div>
div3
</div>
</div>