How can I use the HTML parser with Apache Tika in Java to extract all HTML tags?

前端 未结 2 1950
温柔的废话
温柔的废话 2021-02-03 14:05

I download tika-core and tika-parser libraries, but I could not find the example codes to parse HTML documents to string. I have to get rid of all html tags of source of a web p

2条回答
  •  梦谈多话
    2021-02-03 14:12

    Do you want a plain text version of a html file? If so, all you need is something like:

            InputStream input = new FileInputStream("myfile.html");
            ContentHandler handler = new BodyContentHandler();
            Metadata metadata = new Metadata();
            new HtmlParser().parse(input, handler, metadata, new ParseContext());
            String plainText = handler.toString();
    

    The BodyContentHandler, when created with no constructor arguments or with a character limit, will capture the text (only) of the body of the html and return it to you.

提交回复
热议问题