我有一个像这样的HTML代码:
<div class="address">
<strong>Max Mustermann </strong>
<br>Secondstreet 12
<br>1234 New York
<br>
<br>
<br>
</div>
这是我的代码:
html = html.replace("<br>", "br34k");
Document doc = Jsoup.parse(html);
Elements divs = doc.select("div.address");
StringBuilder divResult = new StringBuilder();
for (Element div : divs) {
divResult.append(div.text());
}
String result = divResult.toString();
result = completeResults.replace("br34k", System.getProperty("line.separator"));
System.out.println(result);
这样输出如下:
06-18 20:00:30.290: I/System.out(623): Cafe Palio
06-18 20:00:30.290: I/System.out(623): Marktplatz 1
06-18 20:00:30.290: I/System.out(623): 79312 Emmendingen
06-18 20:00:30.290: I/System.out(623):
06-18 20:00:30.290: I/System.out(623):
06-18 20:00:30.300: I/System.out(623): Domino Stüble
06-18 20:00:30.300: I/System.out(623): Markgrafenstr. 57
06-18 20:00:30.300: I/System.out(623): 79312 Emmendingen
06-18 20:00:30.300: I/System.out(623):
06-18 20:00:30.300: I/System.out(623):
06-18 20:00:30.300: I/System.out(623): Pizza Boxx
06-18 20:00:30.300: I/System.out(623): Am Elzdamm 66
06-18 20:00:30.300: I/System.out(623): 79312 Emmendingen
但我需要的是没有名称的String,例如:
Marktplatz 1 79312埃门丁根
Markgrafenstr。 57 79312埃门丁根
依旧......
答案 0 :(得分:0)
如果你的HTML标记是正确的,那将是最简单的:
<div class="address">
<strong id="name">Max Mustermann </strong>
<span id="address-part-one">Secondstreet 12</span>
<span id="address-part-two">1234 New York</span>
</div>
此处不需要br
标记,您应该使用CSS代替。分别检索地址标记内容然后连接它。
答案 1 :(得分:0)
如果需要,您可以围绕String
优化代码。
Document document = Jsoup.parse(content);
String text = document.select(".address").text();
String title = document.select(".address strong").text();
String output = text.replaceFirst(title, "").trim();
System.out.println(output);
更新了答案以反映更新的问题
如果您有多个<div>
与class="address"
Elements elements = document.select(".address");
for (Iterator<Element> iterator = elements.iterator(); iterator.hasNext();)
{
Element element = iterator.next();
String text = element.text();
String title = element.select("strong").text();
String output = text.replaceFirst(title, "").trim();
System.out.println(output);
}