在Java中的HTML截断 [英] html truncator in java
问题描述
在Java中是否有截断HTML(用于预览)的实用工具(或示例源代码)?我想在服务器端进行截断,而不是在客户端进行截断。
Is there any utility (or sample source code) that truncates HTML (for preview) in Java? I want to do the truncation on the server and not on the client.
我使用HTMLUnit来解析HTML。
I'm using HTMLUnit to parse HTML.
UPDATE:
我希望能够预览HTML,因此截断器将在保留HTML结构的同时在所需输出长度后剥离元素。
UPDATE:
I want to be able to preview the HTML, so the truncator would maintain the HTML structure while stripping out the elements after the desired output length.
推荐答案
我认为您将需要编写自己的XML解析器来完成此任务。拉出身体节点,添加节点直到二进制长度<一些固定的大小,然后重建文件。如果HTMLUnit没有创建语义XHTML,我会推荐 tagsoup 。
I think you're going to need to write your own XML parser to accomplish this. Pull out the body node, add nodes until binary length < some fixed size, and then rebuild the document. If HTMLUnit doesn't create semantic XHTML, I'd recommend tagsoup.
如果您需要XML解析器/处理程序,建议使用 XOM 。
If you need an XML parser/handler, I'd recommend XOM.
这篇关于在Java中的HTML截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!