JSOUP-Java在DOM中创建一个新标记
我试图使用Jsoup在本地网页上的一些现有“区域”之间添加一个新标记“区域” 我的代码如下:JSOUP-Java在DOM中创建一个新标记,java,tags,jsoup,area,Java,Tags,Jsoup,Area,我试图使用Jsoup在本地网页上的一些现有“区域”之间添加一个新标记“区域” 我的代码如下: <hmtl> <head> ... </head> <body> ...a lot of <div>.. <map name="diagram_map"> <area id="area1" /> <!-- I would like to insert a new area here-->
<hmtl>
<head>
...
</head>
<body>
...a lot of <div>..
<map name="diagram_map">
<area id="area1" />
<!-- I would like to insert a new area here-->
<area id="area2" />
</map>
</body>
<hmtl/>
但是我不知道如何添加一个新的“区域”标记,我尝试了所有的append,after/before,但它在标记“area”中写入了新标记
提前谢谢 试试看
import java.io.File;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class JsoupFrame {
public static void main(String[] args) throws Exception {
Document doc = Jsoup.parse(new File("t2.html"), "UTF-8");
doc.select("area#area1").after("<area id=\"newArea\" />");
System.out.println(doc.html());
}
}
导入java.io.File;
导入org.jsoup.jsoup;
导入org.jsoup.nodes.Document;
公共类JsoupFrame{
公共静态void main(字符串[]args)引发异常{
documentdoc=Jsoup.parse(新文件(“t2.html”),“UTF-8”);
文件选择(“”)后的(“区域#区域1”);
System.out.println(doc.html());
}
}
Html
很多
输出
<html>
<head></head>
<body>
<hmtl> ...a lot of
<div>
..
<map name="diagram_map"> <area id="area1"></area><area id="newArea"></area> <area id="area2"></area> </map>
<hmtl />
</div>
</hmtl>
</body>
</html>
很多
..
不起作用,可能是我的医生?我有:filein=新文件(pathWebProject)
Document doc=Jsoup.parse(在“UTF-8”中)代码>这在html中提供了什么?我用您提供的html对它进行了测试,它运行良好(哼,这是返回我,我在选择“区域1”上选择的区域。我的“文档”是正确的吗?谢谢你回答我的完整程序。很抱歉它起作用了!!非常感谢我三个小时以来一直在做这个任务^^
<hmtl>
<head>
</head>
<body>
...a lot of <div>..
<map name="diagram_map">
<area id="area1" />
<area id="area2" />
</map>
</body>
<hmtl/>
<html>
<head></head>
<body>
<hmtl> ...a lot of
<div>
..
<map name="diagram_map"> <area id="area1"></area><area id="newArea"></area> <area id="area2"></area> </map>
<hmtl />
</div>
</hmtl>
</body>
</html>