HTML解析库--JsoupDemo

编码前的准备:

    1、使用Jsoup解析html新闻列表:http://mobile.csdn.net/

 2、使用Jsoup解析Epub 

源代码地址:https://github.com/dpl12/JsoupDemo

创建Android工程JsoupDemo:


添加Jsoup的gradle依赖: 

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'org.jsoup:jsoup:1.10.3'
}

添加权限:

<uses-permission android:name="android.permission.INTERNET"/>


创建assets文件,添加fb.ncx电子书(见源代码地址)


MainActivity.java代码如下:

package com.example.dpl.jsoupdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.InputStream;

/*
1.使用Jsoup解析html新闻列表
2.使用Jsoup解析Epub
 */
public class MainActivity extends AppCompatActivity {
    private String url="http://mobile.csdn.net/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(new Runnable() {
            @Override
            public void run() {
                //parseHtml();//1.使用Jsoup解析html新闻列表
                parseEpub();//2.使用Jsoup解析Epub(电子书)
            }
        }).start();
    }

    private void parseEpub() {
        try {
            InputStream is=getAssets().open("fb.ncx");//获取assets文件的输入流
            int size=is.available();//定义缓冲的大小
            byte[] buffer=new byte[size];//定义缓冲区
            is.read(buffer);//将所有内容读入缓冲区
            is.close();//输入流关闭
            String ePubText=new String(buffer,"UTF-8"); //用缓冲区中的内容创建字符串
            Document document1=Jsoup.parse(ePubText);//Document 对象代表整个 XML 文档
            String docTitle=document1.getElementsByTag("docTitle").first().text();//获取节点docTitle第一条text内容
            Log.i("info",docTitle);
            Elements elements=document1.getElementsByTag("navPoint");//获取节点navPoint
            for (Element ele1:elements){//遍历
                String title=ele1.text();//获取节点navPoint中的text内容
                String href=ele1.getElementsByTag("content").first().attr("src");//获取节点content第一条链接
                Log.i("info",title+":"+href);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void parseHtml() {
        try {
            Document document= Jsoup.connect(url).get();//get方式获取数据
            Elements elements=document.select("div.unit");//获取节点unit
            for (Element ele:elements){
                String title=ele.getElementsByTag("h1").first().text();//获取节点unit内第一条h1标题
                String href=ele.getElementsByTag("h1").first()
                        .getElementsByTag("a").first().attr("href");//获取节点unit内第一条h1超级链接
                Log.i("info",title+":"+href);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行效果:




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值