0

我使用 Jsoup 从网络获取元素:

Elements addresses = doc.select("address > div");

结果是这样的:

<address>
    <div>
    7135 S Kingery Hwy<br>Willowbrook, IL 60527
</div>
<div class="phone">
        (630) 288-6635
</div>
</address>

我很难从标签中检索地址。我使用 text() 方法:

for (Element address : addresses) {
    Log.i("addresses", address.text() );
}

结果是:

7135 S Kingery Hwy Willowbrook, IL 60527
(630) 288-6635

如何过滤它以仅检索地址并将 br 标记替换为换行符?预期结果:

7135 S Kingery Hwy 
Willowbrook, IL 60527
4

1 回答 1

1

你可以试试这个

    Elements addresses = doc.select("address > :not(div[class=phone])");
    for (Element address : addresses) {
        for (Node node : address.childNodes()) {
            if (node.nodeName().equals("br")) {
                continue;
            }
            String text = node.toString().trim();
            System.out.println(text);
        }
    }
于 2012-12-29T04:20:20.523 回答