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 ""; } }