Android平台使用Jsoup解析HTML

public void onParse(View v) {
    InputStream iStream;


    try {
        iStream = mContext.getAssets().open("index.html");

        String html = getStringFromInputStream(iStream);

        Document doc = Jsoup.parse(html);

        Elements elms = doc.getElementsByTag("link");
        Element element = elms.get(0);

        String hr =  element.attr("href");

        if (hr != null) {

            String hrefString = "content://" + hr;


            element.attributes().remove("href");

            element.attributes().put("href", hrefString);


        }

        Log.i("JsoupTage hr = ", hr);
        String contenString = doc.outerHtml();

        Log.i("JsoupTage", contenString);


    } catch (IOException e) {

        e.printStackTrace();
    } //打开文件 


}


// convert InputStream to String
private static String getStringFromInputStream(InputStream is) {
 
    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();
 
    String line;
    try {
 
            br = new BufferedReader(new InputStreamReader(is));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
 
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
        return sb.toString();
 
}


你可能感兴趣的:(Android平台使用Jsoup解析HTML)