使用java修改HTML
我正在尝试阅读一个HTML文件并添加一些文本的链接:
I am trying to read a HTML file and add link to some of the texts :
例如:
我要添加链接到Campaign0 文字。 :
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>
要添加的链接:
<a href="Second.html">
我需要一个修改html的JAVA程序,通过 Campaign0 添加超链接
I need a JAVA program that modify html to add hyperlink over "Campaign0" .
我如何使用Jsoup?
How i do this with Jsoup ?
我用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 :(
em>简而言之 :有没有像 - >
In short : is there anything like-->
if find <span>Campaign0</span>
then replace by <span><a href="">Campaign0</a></span>
使用JSoup或JAVA代码中的任何技术
using JSoup or any technology inside JAVA code??
您的您可以使用JSoup选择器span:containsOwn(Campaign0)来查找Campaign0,Campaign1等的跨度元素。有关JSoup选择器的其他文档,请参阅 jsoup.org 。
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.
找到元素并用链接包装,调用doc.html()应该返回修改后的HTML co de。这是一个工作示例:
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:
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>
代码:
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();
输出:
<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>