Jtidy解析脚本时候出现StringIndexOutOfBoundsException异常问题
来源:互联网 发布:网络维护企业名录 编辑:程序博客网 时间:2024/06/05 08:20
问题描述:
最近在做网页结构化信息抽取,用到了JTidy和xslt。当在处理一些包含很多脚本的页面时候,出现了,JTidy去脏失败,提示标题中的异常。
最后发现,问题出现在解析脚本的时候因为一些脚本里面不规范的内容,导致不能判断结束造成了上面的异常出现。
解决方法:
最初的时候想通过修改JTidy的源码来解决这个问题,但是后来做着发现可行性不高,一个是修改这个源码可能会带来其它的问题。另外一个,还要花长时间去看源码。
所以,最终还是选择了采用预处理的方式来进行处理删除掉脚本。
代码
public static String getFilterBody(String strBody) {// htmlparser 解析Parser parser = Parser.createParser(strBody, "utf-8");NodeList list;String reValue = strBody;try {list = parser.parse(null);visitNodeList(list);reValue = list.toHtml();} catch (ParserException e1) {}return reValue;}// 递归过滤private static void visitNodeList(NodeList list) {for (int i = 0; i < list.size(); i++) {Node node = list.elementAt(i);if (node instanceof Tag) {if (node instanceof ScriptTag) {list.remove(i);continue;}// 这里可以增加删除的Tagif (node instanceof StyleTag) {list.remove(i);continue;}// 这里可以增加删除的Tag}NodeList children = node.getChildren();if (children != null && children.size() > 0)visitNodeList(children);}}
但是在删除脚本的时候一样遇到了相同的问题,就是在解析脚本的时候出现了错乱,把一些脚本中的标签识别为正常标签。
如:<script>里面的 '<span></span>'里面的‘</’就会被识别为脚本的结束,导致脚本获取不全,删除不全
最后在网上找到了解决的办法
通过下面两个参数的设置来解析了html对脚本的处理问题
org.htmlparser.scanners.ScriptScanner.STRICT = false;org.htmlparser.lexer.Lexer.STRICT_REMARKS = false;
只要配置其中之一就可以了,下面是这两个参数的一个官方说明
org.htmlparser.scanners.ScriptScanner.STRICT = false;
/** * Strict parsing of CDATA flag. * If this flag is set true, the parsing of script is performed without * regard to quotes. This means that erroneous script such as: * <pre> * document.write("</script>"); * </pre> * will be parsed in strict accordance with appendix * <a href="http://www.w3.org/TR/html4/appendix/notes.html#notes-specifying-data" mce_href="http://www.w3.org/TR/html4/appendix/notes.html#notes-specifying-data"> * B.3.2 Specifying non-HTML data</a> of the * <a href="http://www.w3.org/TR/html4/" mce_href="http://www.w3.org/TR/html4/">HTML 4.01 Specification</a> and * hence will be split into two or more nodes. Correct javascript would * escape the ETAGO: * <pre> * document.write("<//script>"); * </pre> * If true, CDATA parsing will stop at the first ETAGO ("</") no matter * whether it is quoted or not. If false, balanced quotes (either single or * double) will shield an ETAGO. Beacuse of the possibility of quotes within * single or multiline comments, these are also parsed. In most cases, * users prefer non-strict handling since there is so much broken script * out in the wild. */
org.htmlparser.lexer.Lexer.STRICT_REMARKS = false;
/** * Process remarks strictly flag. * If <code>true</code>, remarks are not terminated by ---$gt; * or --!$gt;, i.e. more than two dashes. If <code>false</code>, * a more lax (and closer to typical browser handling) remark parsing * is used. * Default <code>true</code>. */
在默认情况下,htmlparser解析是按严格的html标准解析,所以当碰到不标准的标签有可能出错,
当把以上这两个参数改变以后,htmlparser解析不再严格,能应对所有可能出现的情况。
- Jtidy解析脚本时候出现StringIndexOutOfBoundsException异常问题
- 制作.sql脚本时候出现的问题!
- mybatis generator StringIndexOutOfBoundsException异常
- Android StartActivity时候出现的requires new task 异常解析
- 执行shell脚本时候出现的小问题
- JTidy
- 解析数据封装对象的时候出现重名问题
- StringIndexOutOfBoundsException
- StringIndexOutOfBoundsException
- 【角标越界异常】ArrayIndexOutOfBoundsException和StringIndexOutOfBoundsException
- NekoHTML和JTIDY解析器简介
- 有关于用JTidy处理HTML转化为标准的XHTML或者XMl出现空的问题
- Spring boot 在执行sql 脚本的时候,会出现中文乱码问题
- 在使用spring quartze进行任务调度的时候,会出现jobDetail的异常问题
- ViewPager在大数量的Item的时候出现的滑动异常的问题
- Hadoop格式化namenode时候,出现Java异常:URI has an authority component问题的解决
- JDiy与DWR组合的时候出现网页无法访问(404异常)的问题
- 使用json的时候出现NestableRuntimeException异常
- 【淘宝&SEO】淘宝官方SEO资料
- Tomcat 配置出现的问题
- FLEX中的Session管理
- iphone游戏开发之cocos2d ( 十 )使用纹理图册,更方便生成精灵动画
- linux 进程查看命令
- Jtidy解析脚本时候出现StringIndexOutOfBoundsException异常问题
- Z-STACK之cc2530LED驱动详解
- 利用 GDB Debug Android SurfaceFlinger
- OpenCV 编程入门
- 自己收集的c语言学习网站
- android中用XMPP Asmack获取用户朋友的presence(名单列表)的实现
- JAVASCRIPT文件操作
- Android内存管理
- 杭电 2037