[英]How to retrieve a String from a tag element using Jsoup?
I use a Jsoup to get the Elements from a web: 我使用Jsoup从Web获取元素:
Elements addresses = doc.select("address > div");
and the result is like this: 结果是这样的:
<address>
<div>
7135 S Kingery Hwy<br>Willowbrook, IL 60527
</div>
<div class="phone">
(630) 288-6635
</div>
</address>
I have a hard time to retrieve the address from the tag. 我很难从标签中检索地址。 I use a text() method:
我使用text()方法:
for (Element address : addresses) {
Log.i("addresses", address.text() );
}
and the result is: 结果是:
7135 S Kingery Hwy Willowbrook, IL 60527
(630) 288-6635
How can I filter it to retrieve the address only and also replace br tag with newline? 如何过滤它以仅检索地址,并用换行符替换br标签? Expected result:
预期结果:
7135 S Kingery Hwy
Willowbrook, IL 60527
You can try this, 你可以试试看
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.