【问题标题】:Jsoup - How to extract every elementsJsoup - 如何提取每个元素
【发布时间】:2013-11-07 08:57:08
【问题描述】:

我正在尝试使用 Jsoup 获取字体信息。例如:

下面是我的代码:

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>

【问题讨论】:

    标签: java html-parsing jsoup


    【解决方案1】:

    如果您只需要从文档中提取文本以及任何 &lt;b&gt; 或 &lt;i&gt; 标签(根据您的示例),请考虑使用 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());
    }
    

    【讨论】:

    • 知道如何将其拆分为数组或数组列表。喜欢array[0] = &lt;b&gt;Hello World&lt;/b&gt;、array[1] = , Testing、array [2]= i&gt;&lt;b&gt;Font &lt;/b&gt;&lt;/i&gt;
    • 查看我的新代码,将每个代码添加到 ArrayList。让我知道它是否必须是 String[] 数组...
    • 我已经编辑了我的问题。 Elements e = doc.select("span"); 无法生成输出。请指教。
    【解决方案2】:

    您需要将选择器更改为 &lt;p&gt; 标记,如下所示:
    Element all = doc.select("p").first();

    那么你需要获取该元素的所有子元素。

    String myString = "";
    for(Element item : all.children()) {
        myString += item.text();
    }
    

    我假设您想要标签内的文本,而不是标签本身。

    您也可以这样做。

    Elements all = doc.select("b");
    all.addAll(doc.select("i"));
    all.addAll(doc.select("span"));
    String myString = all.text();
    

    【讨论】:

    • 在我的代码中 all 仅引用 bold 值,但我想做每个元素,而不仅仅是粗体
    • 我明白了,那么您希望您的选择器成为

      标签并获取其中的所有子元素,我将更新我的答案以显示

    • 错误= no suitable method found for add(Elements) method Elements.add(int,Element) is not applicable (actual and formal argument lists differ in length) method Elements.add(Element) is not applicable (actual argument Elements cannot be converted to Element by method invocation conversion) ----
    • 抱歉是addAll 不是add
    猜你喜欢
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多