Jsoup学习笔记九:Jsoup 解析saz文件,读取其中的htm文件到字符串,提取字符串中的数据写入csv文件中

Jsoup学习笔记9:Jsoup 解析saz文件,读取其中的htm文件到字符串,提取字符串中的数据写入csv文件中

本篇笔记将上篇笔记的操作做些改进,不再把saz文件中的htm文件解析出来,而是不解压直接读取其中的数据成字符串,基本思路如下:

1、自定义一个从文本文件读取内容到字符串的类:解析saz文件中的htm文档,将文件的内容读取到字符串中

2、自定义利用Jsoup解析htm字符串的类:利用Jsoup解析传入的htm字符串,将解析结果写入csv文件中

3、解析时,指定好文件路径,直接调用上面的两个工具类即可

示例代码如下:

package com.daxiang.saztest;

/**
 * 自定义一个从文本文件读取内容到字符串的类
 * @author daxiang
 * @date 2015-7-23
 * @Description: 解析saz文件中的htm文档,将文件的内容读取到字符串中
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ReadSazToString {

	/**
	 * 读取saz文件的方法
	 * @param buffer buffer
     * @param filePath 文件路径
     * @throws IOException 异常
	 */
	public static void readToBuffer(StringBuffer buffer, String filePath)
			throws IOException {

		File file = new File(filePath); // 找到压缩文件
		ZipFile zipFile = new ZipFile(file); // 实例化ZipFile对象
		System.out.println("要解析的saz文件名称:" + zipFile.getName()); // 得到saz文件的名称

		ZipEntry ze = zipFile.getEntry("_index.htm"); // 得到saz文件中的压缩实体_index.htm
		System.out.println("file - " + ze.getName() + " : " + ze.getSize() + " bytes");

		long size = ze.getSize();
		if (size > 0) {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					zipFile.getInputStream(ze)));
			String line; // 用来保存每行读取的内容
			line = reader.readLine(); // 读取第一行
			while (line != null) { // 如果 line 为空说明读完了
				buffer.append(line); // 将读到的内容添加到 buffer 中
				buffer.append("\n"); // 添加换行符
				line = reader.readLine(); // 读取下一行
			}
			reader.close();
		}

		zipFile.close();
	}

	/**
	 * 将文件内容读取到字符串的方法
	 */
	public static String readFile(String filePath) throws IOException {
		StringBuffer sb = new StringBuffer();
		ReadSazToString.readToBuffer(sb, filePath);
		return sb.toString();
	}
}
package com.daxiang.saztest;
/**
 * 自定义利用Jsoup解析htm字符串的类
 * @author daxiang
 * @date 2015-7-23
 * @Description: 利用Jsoup解析传入的htm字符串,将解析结果写入csv文件中
 */
import java.io.FileWriter;
import java.io.IOException;

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


public class JsoupStrToCsv {

	/** 
     * 解析htm文档,将解析数据写入csv文件的方法 
     * @param htmStr 传入的htm字符串
     * @param csvPath 生成的csv文件路径
     * @throws IOException 异常
     */  
	public static void JsoupStr(String htmStr, String csvPath)
			throws IOException {
		// 导入解析出来的htm文档字符串
		Document doc = Jsoup.parse(htmStr);
		// 提取表头信息
		Elements heads = doc.getElementsByTag("table").select("thead");
		FileWriter fw = new FileWriter(csvPath);
		for (int m = 0; m < heads.size(); m++) {
			Elements head = heads.get(m).select("th");
			for (int n = 0; n < head.size(); n++) {
				// 经过观察,该htm文档table标签内共有23列数据,因为只需要第2至12列的数据,所以其他列排除掉不予提取
				if (n == 0) {
					continue;
				} else if (n < 22) {
					// text()方法和toString()方法的效果不同
					// String h = head.get(n).toString() + "\r\n";
					String h = head.get(n).text() + ",";
					fw.write(h);
					//System.out.print(h);
				} else if (n == 22) {
					String h = head.get(n).text() + "\r\n";
					fw.write(h);
					//System.out.print(h);
				} else {
					break;
				}
			}
		}
		// 提取表格信息
		Elements trs = doc.getElementsByTag("table").select("tr");
		for (int i = 0; i < trs.size(); i++) {
			Elements tds = trs.get(i).select("td");
			for (int j = 0; j < tds.size(); j++) {
				// 把空格无内容的部分去除掉
				// if(!"".equals(tds.get(j).text())){
				// String str = tds.get(j).text() + "\r\n";
				// System.out.print(str);
				// fw.write(str);
				// }
				if (j == 0) {
					continue;
				} else if (j < 22) {
					// 加双引号的作用很大,有些数据是包含逗号的,不处理一下,本来在一个表格里的数据
					// 会因为逗号的存在分开到好几列表格中
					String str1 = "\"" + tds.get(j).text() + "\"";
					String str2 = str1 + ",";
					// System.out.print(str2);
					fw.write(str2);
				} else if (j == 22) {
					String str3 = tds.get(j).text() + "\r\n";
					// System.out.print(str3);
					fw.write(str3);
				} else {
					break;
				}
			}
		}
		fw.flush();
		fw.close();
	}
}
package com.daxiang.saztest;

/**
 * @Author: 大象Jepson
 * @Date: 2015-7-23
 * @Email: chenjinpeng0326@163.com
 * @Version: Version1.0
 * @CopyRight: 大象Jepson
 * @Description: 解析saz文件,不解压,直接读取其中的htm文件到字符串,
 * 				  利用Jsoup解析生成的htm文件,提取其中table标签内的数据,将解析结果写入csv文件中
 */

public class SazJsoupTest {

	static String url = "D:\\daxiang\\saztest\\21316.saz";
	
	public static void main(String[] args) throws Exception {
	
		// 定义存放生成csv文件的路径
		String csvPath = "D:\\daxiang\\saztest\\21316.csv";
		// 调用ReadFileToString类中的readFile方法
		String htmStr = ReadSazToString.readFile(url);
		// 调用JsoupStrToCsv类中的JaoupStr方法
		JsoupStrToCsv.JsoupStr(htmStr, csvPath);
		
		System.out.println("saz文件解析完成!");  
	}
}




版权声明:本文为博主原创文章,未经博主允许不得转载。