惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
月光博客
月光博客
IT之家
IT之家
B
Blog RSS Feed
T
Tailwind CSS Blog
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
博客园_首页
B
Blog
V
V2EX
腾讯CDC
Vercel News
Vercel News
量子位
Microsoft Security Blog
Microsoft Security Blog

博客园 - 夜隼

nohup启动java命令导致dubbo无法注册 SOA架构改造简单记录 [转]BloomFilter——大规模数据处理利器 IOS行货自动打包 Kruskal算法java版 prim算法java版 利用Word的宏录制来反查一些菜单操作的命令 Spring MVC 数据类型转换 Xpath[转] javascript阻塞加载问题【转】 大屏幕手机上网页字体显示很小的问题 The connection to adb is down, and a severe error has occured. 服务器宕机排查记录 远程桌面截图出现黑屏的解决办法 Mutex IE参考 SWT的Display 肩部肌肉劳损zt [ZT]使用tmpfs缓存文件提高性能
nekohtml转换html时标签变大写的问题
夜隼 · 2018-03-23 · via 博客园 - 夜隼
public static Document transferByNeko(InputStream stream, String charset)
    {
        if (stream == null)
            return null;
        
        if(StringUtils.isEmpty(charset)){
            charset = DEFAULT_CHARSET;
        }


        //NEKOHTML的DOMParser会将html标签转化成大写,是否设置下面的配置都没有意义,解决办法是需要使用xerces的DOMParser
//        DOMParser domParser = new DOMParser();
//        Document doc = null;
//        ByteArrayOutputStream byteOs = null;
//        Writer writer = null;
//        InputSource inputSource = null;
//        DocumentType documentType = null;
//        org.w3c.dom.Document document = null;
//        DOMReader domReader = null;
//        try {
//            domParser.setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
//            domParser.setProperty("http://cyberneko.org/html/properties/names/attrs", "lower");
//            domParser.setProperty("http://cyberneko.org/html/properties/default-encoding", "UTF-8");
//
//            domParser.setFeature("http://xml.org/sax/features/namespaces", false);
//            domParser.setFeature("http://cyberneko.org/html/features/balance-tags", true);
//            domParser.setFeature("http://cyberneko.org/html/features/scanner/script/strip-comment-delims", false);
//
//            byteOs = new ByteArrayOutputStream();
//            writer = new Writer(byteOs, charset);
//            XMLDocumentFilter domFilter[] = {
//                writer
//            };
//            domParser.setProperty("http://cyberneko.org/html/properties/filters", domFilter);
//            inputSource = new InputSource(new InputStreamReader(stream, Charset.forName(charset)));
//            domParser.parse(inputSource);
//            document = domParser.getDocument();
//            documentType = document.getDoctype();
//            if (documentType != null)
//                document.removeChild(documentType);
//            domReader = new DOMReader();
//            doc = domReader.read(document);
//        } catch (SAXNotRecognizedException e) {
//            e.printStackTrace();
//        } catch (SAXNotSupportedException e) {
//            e.printStackTrace();
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        } catch (SAXException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }finally{
//            IOUtils.closeQuietly(byteOs);
//            IOUtils.closeQuietly(stream);
//        }

        //采用xerces的DOMParser
        Document doc = null;
        DocumentType documentType = null;
        org.w3c.dom.Document document = null;
        DOMReader domReader = null;
        ByteArrayOutputStream byteOs = null;
        Writer writer = null;
        InputSource inputSource = null;
        try {
            HTMLConfiguration htmlConfiguration = new HTMLConfiguration();
            htmlConfiguration.setProperty("http://cyberneko.org/html/properties/names/elems","lower");
            org.apache.xerces.parsers.DOMParser parser = new org.apache.xerces.parsers.DOMParser(htmlConfiguration);
            inputSource = new InputSource(new InputStreamReader(stream, Charset.forName(charset)));
            parser.parse(inputSource);
            document = parser.getDocument();
            documentType = document.getDoctype();
            if (documentType != null)
                document.removeChild(documentType);
            domReader = new DOMReader();
            doc = domReader.read(document);
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return doc;
    }