java - Jsoup - 如何提取每个元素

标签 java html-parsing jsoup

我正在尝试使用 Jsoup 获取字体信息。举个例子: Sample font

下面是我的代码:

result = rtfToHtml(new StringReader(streamToString((InputStream)contents.getTransferData(dfRTF))));
                // Example of text extraction from html
                // Parse html
                // String test = result.toString();
                Document doc = Jsoup.parse(result);
                // Select first bold text
                String strdoc = doc.toString();
                String words[] = strdoc.split("font-family");
                Element firstBoldElt = doc.select("b").first(); 
                Elements ele = doc.select("body");
                String test = ele.toString();
                Elements all = doc.select("b");
                String boldtext = all.text();

通过使用代码,我的输出将如下所示:

"<body> 
 <p class="default">
     <span style="color: #000000; font-size: 21pt; font-family: MyriadPro-Bold;">
         <b>Hello World</b>
     </span>
     <span style="color: #000000; font-size: 21pt; font-family: MyriadPro-Bold;">, Testing</span> 
     <span style="color: #000000; font-size: 21pt; font-family: MyriadPro-Bold;">
         <i><b>Font </b></i>
     </span>
     <span style="color: #000000; font-size: 21pt; font-family: MyriadPro-Bold;"> Style</span>
     <span style="color: #000000; font-size: 21pt; font-family: MyriadPro-Bold;">
         <i>Check</i>
     </span>
     <span style="color: #000000; font-size: 10pt; font-family: MyriadPro-Bold;"></span>
</p>   
</body>"

我可以提取第一个 BOLD 元素或所有 BOLD 元素,但如何提取所有类似的元素。

<b>Hello World</b>
, Testing
<i><b>Font </b></i>
 Style 
<i>Check</i> 

非常感谢任何建议或引用。
已编辑

<body lang="en-MY" dir="LTR"> 
 <p style="margin-bottom: 0in">
 <font color="#000000"> <font face="ArialMT, serif"> <font size="2">
 <span style="font-style: normal">
 <span style="text-decoration: none">
 <b>BOLD </b>
 </span>
 </span>
 </font></font></font>
 <font color="#000000"><font face="ArialMT, serif"><font size="2">
 <span style="font-style: normal">
 <span style="text-decoration: none">
 <span style="font-weight: normal">
 REGULAR 
 </span>
 </span>
 </span>
 </font></font></font>
 <font color="#000000"><font face="ArialMT, serif"><font size="2">
 <span style="font-style: normal">
 <u>
 <span style="font-weight: normal">
 UNDERLINED
 </span>
 </u>
 </span>
 </font></font></font>
 <font color="#000000"><font face="ArialMT, serif"><font size="2">
 <span style="font-style: normal">
 <span style="text-decoration: none">
 <span style="font-weight: normal"> 
 </span>
 </span>
 </span>
 </font></font></font>
 <font color="#000000"><font face="ArialMT, serif"><font size="2">
 <i>
 <span style="text-decoration: none">
 <span style="font-weight: normal">
 ITALIC
 </span>
 </span>
 </i>
 </font></font></font>
 <font color="#000000"><font face="ArialMT, serif"><font size="2">
 <span style="font-style: normal">
 <span style="text-decoration: none">
 <span style="font-weight: normal"> 
 </span>
 </span>
 </span>
 </font></font></font>
 <font color="#000000"><font face="ArialMT, serif"><font size="2">
 <i>
 <span style="text-decoration: none">
 <b>BOLDITALIC</b>
 </span>
 </i></font>
 </font></font></p>  
</body>

最佳答案

如果只需要从文档中提取文本,则加上任意 <b><i>标签(根据您的示例),请考虑使用 Whitelist 类(请参阅 docs ):

String html = "<body><p class='default'> <span style='color: #000000; font-size: 21pt; font-family: MyriadPro-Bold;'> <b>Hello World</b> </span> <span style='color: #000000; font-size: 21pt; font-family: MyriadPro-Bold;'> , Testing </span> <span style='color: #000000; font-size: 21pt; font-family: MyriadPro-Bold;'> <i><b>Font </b></i> </span> <span style='color: #000000; font-size: 21pt; font-family: MyriadPro-Bold;'> Style </span> <span style='color: #000000; font-size: 21pt; font-family: MyriadPro-Bold;'> <i>Check</i> </span> <span style='color: #000000; font-size: 10pt; font-family: MyriadPro-Bold;'> </span> </p></body>";

Whitelist wl = Whitelist.simpleText();
wl.addTags("b", "i"); // add additional tags here as necessary
String clean = Jsoup.clean(html, wl);
System.out.println(clean);  

它将输出(根据您的示例):

11-07 19:04:45.738: I/System.out(318): <b>Hello World</b>   , Testing   
11-07 19:04:45.738: I/System.out(318): <i><b>Font </b></i>   Style   
11-07 19:04:45.738: I/System.out(318): <i>Check</i>

更新:

ArrayList<String> elements = new ArrayList<String>();

Elements e = doc.select("span");

for (int i = 0; i < e.size(); i++) {
    elements.add(e.get(i).html());
}

关于java - Jsoup - 如何提取每个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19831558/

相关文章:

java - 公共(public)集合缓冲区容量

java - 在 Linux 中获取用户进程的执行时间

ruby - 使用 Nokogiri 从链接获取 href 时无法将字符串转换为整数

java - Jsoup 设置接受语言

java - 根据行数据从表行中提取值 - JSoup

java - 如何修复NullPointerException,jsoup元素

检测有效网页的Java函数

java - 具有 FreeMarker 2.3.23 依赖项的 Maven 项目无法编译

php - 检查链接交换

html - 从 HTML 加载 DOM : How does a HTML parser know when empty element ends