gpt4 book ai didi

java - 使用 Jsoup 库解析具有键值对的字符串

转载 作者:行者123 更新时间:2023-11-30 08:27:13 24 4
gpt4 key购买 nike

我一直坚持如何以键值对的形式解析这些数据。请指导我

<div class="content">
<div class="label">Company Name: </div>
Cartell Chemical Co., Ltd.
<br/>
<div class="label">Business Owner: </div>
Michael Chen
<br/>
<div class="label">Employees: </div>
210
<br/>
<div class="label">Main markets: </div>
North America, Europe, China, South Asia
<br/>
<div class="label">Business Type: </div>
Manufacturer
<br/>
</div>

我需要这些格式的输出。请指导我使用 Java 和 Jsoup 库

Company Name:Cartell Chemical Co., Ltd.
Business Owner:Michael Chen
Employees:210
Main markets:North America, Europe, China, South Asia
Business Type:Manufacturer

最佳答案

查看文档。

这是一个工作示例:

public class StackOverflow20973268 {
private static String input = "<div class=\"content\">" +
"<div class=\"label\">Company Name: </div>" +
"Cartell Chemical Co., Ltd." +
"<br/>" +
"<div class=\"label\">Business Owner: </div>" +
"Michael Chen" +
"<br/>" +
"<div class=\"label\">Employees: </div>" +
"210" +
"<br/>" +
"<div class=\"label\">Main markets: </div>" +
"North America, Europe, China, South Asia" +
"<br/>" +
"<div class=\"label\">Business Type: </div>" +
"Manufacturer" +
"<br/>" +
"</div>";

public static void main(String[] args) throws IOException {
Document doc = Jsoup.parse(input);
Elements labels = doc.select("div.content div.label");
for (Element label : labels) {
System.out.println(String.format("%s:%s", label.text().trim(),
label.nextSibling().outerHtml()));
}
}
}

输出:

Company Name::Cartell Chemical Co., Ltd.
Business Owner::Michael Chen
Employees::210
Main markets::North America, Europe, China, South Asia
Business Type::Manufacturer

关于java - 使用 Jsoup 库解析具有键值对的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20973268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com