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

时间:2009-12-19 21:56:32

标签: java html tags jtidy

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

之前:

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

后:

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

我尝试了JTidy,但它不支持getTextContent。还有其他解决方案吗?


谢谢,我希望解析没有格式良好的HTML。我试过TagSoup,但是当我有这个代码时:

<body>
sometext <div>text</div>
</body>

我希望将“sometext”更改为“someAnotherText”,当我使用{bodyNode}.getTextContent()时,它会给我:“sometext text”;当我使用setTextContet("someAnotherText"+{bodyNode}.getTextContent())并序列化这些结构时,结果是<body>someAnotherText sometext text</body>,没有<div>标记。这对我来说是一个问题。

4 个答案:

答案 0 :(得分:6)

除非您完全确定HTML有效并且格式正确,否则我强烈建议您使用HTML解析器,例如TagSoupJerichoNekoHTML,{ {3}}等,这两个首先特别强大,可以解析任何类型的垃圾:)

例如,使用HTML Parser(因为实施非常简单),使用HTML Parser,提供您自己的visitor

public class MyNodeVisitor extends NodeVisitor {
    public MyNodeVisitor() {
    }

    public void visitStringNode (Text string)
    {
        if (string.getText().equals("**text**")) {
            string.setText("**new text**");
        }
    }
}

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

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

这只是实现这一目标的一种方式,非常简单。

答案 1 :(得分:4)

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

如果您的文本是id =“id”的节点的唯一子节点,那么这样的操作将会起作用:

Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
Element e = d.getElementById("id");
Node text = e.getFirstChild();
text.setNodeValue(process(text.getNodeValue());

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

答案 2 :(得分:0)

列出了一堆开源Java HTML解析器here

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

答案 3 :(得分:0)

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

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

示例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>

Java代码:

     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>