Modifying HTML using java

2019-02-18 16:17发布

I am trying to read a HTML file and add link to some of the texts :

for example : I want to add link to "Campaign0" text. :

<td><p style="overflow: hidden; text-indent: 0px; "><span style="font-family: SansSerif;">101</span></p></td> 
<td><p style="overflow: hidden; text-indent: 0px; "><span style="font-family: SansSerif;">Campaign0</span>
<td><p style="overflow: hidden; text-indent: 0px; "><span style="font-family: SansSerif;">unknown</span></p></td>

Link to be added:

<a href="Second.html">

I need a JAVA program that modify html to add hyperlink over "Campaign0" .

How i do this with Jsoup ?

I tried this with JSoup :

        File input = new File("D://First.html");
        Document doc = Jsoup.parse(input, "UTF-8", "");
        Element span = doc.select("span").first(); <-- this is only for first span tag :(
        span.wrap("<a href="Second.html"></a>");

Is this correct ?? It's not working :(

In short : is there anything like-->

 if find <span>Campaign0</span> 
 then replace by <span><a href="">Campaign0</a></span> 

using JSoup or any technology inside JAVA code??

1条回答
我只想做你的唯一
2楼-- · 2019-02-18 16:54

Your code seems pretty much correct. To find the span elements with "Campaign0", "Campaign1", etc., you can use the JSoup selector "span:containsOwn(Campaign0)". See additional documentation for JSoup selectors at jsoup.org.

After finding the elements and wrapping them with the link, calling doc.html() should return the modified HTML code. Here's a working sample:

input.html:

<table>
    <tr>
        <td><p><span>101</span></p></td>
        <td><p><span>Campaign0</span></p></td>
        <td><p><span>unknown</span></p></td>
    </tr>
    <tr>
        <td><p><span>101</span></p></td>
        <td><p><span>Campaign1</span></p></td>
        <td><p><span>unknown</span></p></td>
    </tr>
</table>

Code:

    File input = new File("input.html");
    Document doc = Jsoup.parse(input, "UTF-8", "");
    Element span = doc.select("span:containsOwn(Campaign0)").first();
    span.wrap("<a href=\"First.html\"></a>");
    span = doc.select("span:containsOwn(Campaign1)").first();
    span.wrap("<a href=\"Second.html\"></a>");
    String html = doc.html();
    BufferedWriter htmlWriter =
            new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output.html"), "UTF-8"));
    htmlWriter.write(html);
    htmlWriter.close();

output:

<html>
 <head></head>
 <body>
  <table> 
   <tbody>
    <tr> 
     <td><p><span>101</span></p></td> 
     <td><p><a href="First.html"><span>Campaign0</span></a></p></td> 
     <td><p><span>unknown</span></p></td> 
    </tr> 
    <tr> 
     <td><p><span>101</span></p></td> 
     <td><p><a href="Second.html"><span>Campaign1</span></a></p></td> 
     <td><p><span>unknown</span></p></td> 
    </tr> 
   </tbody>
  </table>
 </body>
</html>
查看更多
登录 后发表回答