美文网首页
Android jsoup解析html、ncx文件

Android jsoup解析html、ncx文件

作者: 闫鹏飞写字的地方 | 来源:发表于2017-06-24 14:47 被阅读44次

    android studio 引用jsoup compile 'org.jsoup:jsoup:1.10.3'
    jsoup开发指南(中文版)地址

    解析ncx文件,只截取了文件部分内容

    QQ截图20170624143650.png

    获取所有text标签内容

     /*
        * 解析章节内容
        * */
        /*
        * 解析章节列表
        * */
        public static void getChapter(String ncxFileUrl){
            try {
                File ncxFile = new File(ncxFileUrl);
                if(!ncxFile.exists()){
                    return ;
                }
                Document doc = Jsoup.parse(ncxFile,"UTF-8");
                Elements links = doc.getElementsByTag("text");
                for(Element link : links) {
                    String linkText = link.text();
                    Log.i("MyTest", "linkText:" + linkText);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    结果

    QQ截图20170624144103.png

    解析html文件,只截取了文件部分内容

    QQ截图20170624144234.png

    获取所有p标签内容

      /*
        * 解析章节内容
        * */
        public static void getChapterContent(String htmlFileUrl){
            try {
                File htmlFile = new File(htmlFileUrl);
                if(!htmlFile.exists()){
                    return ;
                }
                Document doc = Jsoup.parse(htmlFile, "UTF-8");
                Elements links = doc.getElementsByTag("p");
                for(Element link : links) {
                    String linkText = link.text();
                    Log.i("MyTest", "linkText:" + linkText);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    结果:

    QQ截图20170624144041.png

    相关文章

      网友评论

          本文标题:Android jsoup解析html、ncx文件

          本文链接:https://www.haomeiwen.com/subject/kddkcxtx.html