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
VERSION:2.1
N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:"; encodeName = qpEncodeing(name); text = text + encodeName; text = text + "
TEL;CELL:" + tel + "
END: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("=
", ""); 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] != '
')) { sb.append(encode[i]); } else if (encode[i] == '=') { sb.append("=3D"); } else if (encode[i] == '
') { sb.append("
"); } 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(); } }