如何在Java中更改HTML标记内容?

如何在Java中更改HTML标记内容?,java,html,tags,jtidy,Java,Html,Tags,Jtidy,如何在Java中更改标记的HTML内容?例如: 之前: <html> <head> </head> <body> <div>text<div>**text**</div>text</div> </body> </html> 我想把“sometext”改为“someAnotherText”,当我使用{bodyNode}.getTe

如何在Java中更改标记的HTML内容?例如:

之前:

<html>
    <head>
    </head>
    <body>
        <div>text<div>**text**</div>text</div>
    </body>
</html>

我想把“sometext”改为“someAnotherText”,当我使用
{bodyNode}.getTextContent()
时,它会给我:“sometext text”;当我使用
settextcontent(“someAnotherText”+{bodyNode}.getTextContent())
并序列化这些结构时,结果是
someAnotherText sometext
,没有
标记。这对我来说是个问题。

如果您的HTML是格式良好的XML(如果不是,那么您可以使用JTidy来整理它),您可以使用DOM或SAX解析器来解析它。如果您的文档不是很大,DOM可能更容易

如果您的文本是id为=“id”的节点的唯一子节点,则类似于这样的操作将起到作用:


之后可以将d保存到文件中。

列出了一系列开源Java HTML解析器


我不确定最常用的是什么,但是(只是称为HTML解析器)可能会做您想做的事情。它具有修改树并将其写回的功能。

除非您绝对确定HTML将有效且格式良好,否则我强烈建议您使用HTML解析器,如、、等,前两种解析器对于解析任何垃圾都特别强大:)

例如,使用(因为实现非常简单),使用,提供您自己的:

然后,创建一个,解析HTML字符串并访问返回的节点列表:

Parser parser = new Parser(htmlString);
NodeList nl = parser.parse(null);
nl.visitAllNodesWith(new MyNodeVisitor());
System.out.println(nl.toHtml());

这只是实现这一点的一种方法,非常简单

通常,您有一个要从中提取数据的HTML文档。您通常知道HTML文档的结构

有几个解析器库,但最好的一个是,您可以使用DOM方法导航文档和更新值。在您的情况下,您需要读取文件并使用属性设置器方法

示例XHTML文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Example</title>
    </head>
    <body>
        <p id="content">Hello World</p>

    </body>
</html>
     File input = new File("D:\\Projects\\Odata Project\\Odata\\src\\web\\html\\inscription_template.xhtml");
            org.jsoup.nodes.Document doc = Jsoup.parse(input,null);
            org.jsoup.nodes.Element content = doc.getElementById("content");
            System.out.println(content.text("Hi How are you ?"));
            System.out.println(content.text());
            System.out.println(doc);
<p id="content">Hi How are you ?</p>
Hi How are you ?
<!--?xml version="1.0" encoding="UTF-8"?-->
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
--><!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
 <head> 
  <title>Example</title> 
 </head> 
 <body> 
  <p id="content">Hi How are you ?</p>   
 </body>
</html>
执行后的输出:

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Example</title>
    </head>
    <body>
        <p id="content">Hello World</p>

    </body>
</html>
     File input = new File("D:\\Projects\\Odata Project\\Odata\\src\\web\\html\\inscription_template.xhtml");
            org.jsoup.nodes.Document doc = Jsoup.parse(input,null);
            org.jsoup.nodes.Element content = doc.getElementById("content");
            System.out.println(content.text("Hi How are you ?"));
            System.out.println(content.text());
            System.out.println(doc);
<p id="content">Hi How are you ?</p>
Hi How are you ?
<!--?xml version="1.0" encoding="UTF-8"?-->
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
--><!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
 <head> 
  <title>Example</title> 
 </head> 
 <body> 
  <p id="content">Hi How are you ?</p>   
 </body>
</html>

你好吗

你好? 例子

你好吗?


我需要更多的澄清,例如getFirstChild();返回节点,那么您如何声明为元素“Element text=e.getFirstChild();”…请澄清@Dmitry…ThanksetNodeValue和getNodeValue是在节点中定义的。感谢您提请注意返回类型。但是您没有定义
进程(…)
方法。您好,我有一个带有链接的html页面。它的URL带有标签rel=“noopener noreferrer”。这些URL不会在onClick事件中打开。你能告诉我怎么解决这个问题吗?
<p id="content">Hi How are you ?</p>
Hi How are you ?
<!--?xml version="1.0" encoding="UTF-8"?-->
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
--><!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
 <head> 
  <title>Example</title> 
 </head> 
 <body> 
  <p id="content">Hi How are you ?</p>   
 </body>
</html>