修改HTML文件,然后获取修改后的html作为输出 [英] Modify HTML file and then get the modified html as an output

查看:79
本文介绍了修改HTML文件,然后获取修改后的html作为输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jsoup的新手,所以我对如何将修改应用于原始HTML文件然后将其作为输出感到困惑.

I am new to jsoup so I am a little bit confused as to how to apply the modification to the original HTML file and then get it as an output.

在通过选择html的部分进行更改之后

After giving changes by selecting the portion of html

例如Element elements = doc.select("_____").attr("_____",____); (因为此元素将仅具有选定的部分...)

e.g. Element elements = doc.select("_____").attr("_____",____); (As this elements would only have the selected portion...)

如何将其应用于原始文档?这样我就可以将修改后的HTML作为输出?

how can I apply this to the original doc? so that I can get the modified HTML as an output?

非常感谢您

推荐答案

所做的更改将应用​​于文档.例如,我从以下开始:

The changes are applied to the doc as you make them. For example, I start off with this:

String html = "<html>" +
        "<body>" +
        "<p class=\"class1\">p1</p>" +
        "<p class=\"class2\">p2</p>" +
        "</body>" +
        "</html>";

Document doc = Jsoup.parse(html);
System.out.println(doc);

它输出:

<html>
 <head></head>
 <body>
  <p class="class1">class1</p>
  <p class="class2">class2</p>
 </body>
</html>

现在,我对p元素进行了一些更改:

Now I make some changes to the p elements:

Element p1 = doc.select("p.class1").first();
p1.attr("class", "classOne");

Element p2 = doc.select("p.class2").first();
p2.attr("id", "helloworld");

System.out.println(doc);

输出有所不同,以反映我对其元素所做的更改:

The output is different to reflect the changes I made to its Elements:

<html>
 <head></head>
 <body>
  <p class="classOne">p1</p>
  <p class="class2" id="helloworld">p2</p>
 </body>
</html>

这篇关于修改HTML文件,然后获取修改后的html作为输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆