java读取柳丁来电xml生成vcf电话联系人

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Test {
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		//	System.out.println(qpDecoding("=E8=81=94=E6=83=B3=E7=A7=BB=E5=8A=A8=E7=83=AD=E7=BA=BF"));
		//	System.out.println(qpEncodeing("联想移动热线"));
		
		SAXReader reader = new SAXReader();
		Document doc = null;
		try {
			doc = reader.read("WebRoot/phone.xml");
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//	List<Node> list =(List<Node>)  doc.selectNodes("//newdingBackup/block/contact");
		Element e = (Element) doc.selectSingleNode("//newdingBackup/block");
		String name = "";
		String tel = "";
		List childnodes = e.elements();
		String pix = "";
		String text = "";
		String encodeName = "";
		for (int i = 0; i < childnodes.size(); i++) {
			Element e1 = (Element) childnodes.get(i);
			name = e1.valueOf("@LastName");
			e1 = e1.element("mobilePhone");
			if (e1 != null)
				tel = e1.valueOf("@value");
			String[] str = tel.split(" ");
			tel = str[0];
			if (i < 10) {
				pix = "00" + i;
			} else if (i < 100) {
				pix = "0" + i;
			} else {
				pix = i + "";
			}
			
			FileWriter fw = new FileWriter("d:/phone/" + name + "_" + pix
					+ ".vcf");//创建FileWriter对象,用来写入字符流
			BufferedWriter bw = new BufferedWriter(fw);
			text = "BEGIN:VCARD\nVERSION:2.1\nN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:";
			encodeName = qpEncodeing(name);
			text = text + encodeName;
			text = text + "\nTEL;CELL:" + tel + "\nEND:VCARD";
			bw.write(text);
			bw.close();
			fw.close();
			
		}
		System.out.println("ok");
	}
	
	/*
	 * 解码
	 */

	public static String qpDecoding(String str) {
		if (str == null) {
			return "";
		}
		try {
			str = str.replaceAll("=\n", "");
			byte[] bytes = str.getBytes("US-ASCII");
			for (int i = 0; i < bytes.length; i++) {
				byte b = bytes[i];
				if (b != 95) {
					bytes[i] = b;
				} else {
					bytes[i] = 32;
				}
			}
			if (bytes == null) {
				return "";
			}
			ByteArrayOutputStream buffer = new ByteArrayOutputStream();
			for (int i = 0; i < bytes.length; i++) {
				int b = bytes[i];
				if (b == '=') {
					try {
						int u = Character.digit((char) bytes[++i], 16);
						int l = Character.digit((char) bytes[++i], 16);
						if (u == -1 || l == -1) {
							continue;
						}
						buffer.write((char) ((u << 4) + l));
					} catch (ArrayIndexOutOfBoundsException e) {
						e.printStackTrace();
					}
				} else {
					buffer.write(b);
				}
			}
			return new String(buffer.toByteArray(), "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/*
	 * 编码
	 */

	public static String qpEncodeing(String str) {
		char[] encode = str.toCharArray();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < encode.length; i++) {
			if ((encode[i] >= '!') && (encode[i] <= '~') && (encode[i] != '=')
					&& (encode[i] != '\n')) {
				sb.append(encode[i]);
			} else if (encode[i] == '=') {
				sb.append("=3D");
			} else if (encode[i] == '\n') {
				sb.append("\n");
			} else {
				StringBuffer sbother = new StringBuffer();
				sbother.append(encode[i]);
				String ss = sbother.toString();
				byte[] buf = null;
				try {
					buf = ss.getBytes("utf-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				if (buf.length == 3) {
					for (int j = 0; j < 3; j++) {
						String s16 = String
								.valueOf(Integer.toHexString(buf[j]));
						// 抽取中文字符16进制字节的后两位,也就是=E8等号后面的两位,
						// 三个代表一个中文字符
						char c16_6;
						char c16_7;
						if (s16.charAt(6) >= 97 && s16.charAt(6) <= 122) {
							c16_6 = (char) (s16.charAt(6) - 32);
						} else {
							c16_6 = s16.charAt(6);
						}
						if (s16.charAt(7) >= 97 && s16.charAt(7) <= 122) {
							c16_7 = (char) (s16.charAt(7) - 32);
						} else {
							c16_7 = s16.charAt(7);
						}
						sb.append("=" + c16_6 + c16_7);
					}
				}
			}
		}
		return sb.toString();
	}
	
}

你可能感兴趣的:(java,xml,IO,cvf,柳丁来电)