【问题标题】:how to read string which is written outside the html <> tags.?如何读取写在 html <> 标签之外的字符串。?
【发布时间】:2014-04-29 11:51:13
【问题描述】:

我有 1000 行的 HTML 代码,我想提取写在 HTML 标签之外的数据。

例如..

<>Java Programm<>

它应该只读取“Java Programm”并转义写在“”标签内的任何内容

我尝试了以下代码,但它正在读取包括 在内的整个数据,但我的输出中不需要“”。

public static void main(String[] args) throws Exception {

    try {
        FileInputStream fin = new FileInputStream("C:\\Users\\File.txt");
        int i;
        while ((i=fin.read())!=-1) {
            System.out.print((char)i);

        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

【问题讨论】:

  • 好的,你正在阅读文本文件。您尝试过什么来实际解决您的特定问题(忽略标签)?
  • @Adriano:OP 没有尝试任何东西,他只是发布了问题。只需复制文件读取代码并进一步为我们..
  • 提示:将其读取为 XML(谷歌在 java 中读取 xml)。使用 element.getTextContent 获取标签外的数据...
  • @Hirak 我没明白..

标签: java html string file-io substring


【解决方案1】:

您需要一个 HTML 解析器。对于 JSoup,它是

File input = new File("C:\\Users\\File.txt");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");   
Element body = doc.body(); //Get the body of the html
System.out.println(body.text()) ; //Get the all the text inside the body tag

这是一种方法。很简单:),当然还有其他方法可以做到这一点。这当然会将文本留在正文标记之外。您可以探索 JSoup a here 并找到解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    相关资源
    最近更新 更多