描述 :根据身份证编号 查询归属地信息。
身份证实体类:
package org.wx.xhelper.model; /** * 身份证实体类 * @author wangxw * @version 1.0 * @date Jul 11, 2014 10:46:54 AM */ public class IdCard { // 身份证号码 private String idCard; // 出生日期 private String born; // 性别 private String sex; // 所在地区 private String att; public String getIdCard() { return idCard; } public void setIdCard(String idCard) { this.idCard = idCard; } public String getBorn() { return born; } public void setBorn(String born) { this.born = born; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAtt() { return att; } public void setAtt(String att) { this.att = att; } }
服务接口类:
package org.wx.xhelper.service; import java.io.UnsupportedEncodingException; import java.net.URL; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.wx.xhelper.model.IdCard; /** * 身份证信息服务接口类 * @author wangxw * @version 1.0 * @date Jul 11, 2014 10:49:57 AM */ public class IdCardService { /** * 生成身份证信息 * @param cardNo * @return 返回身份证信息 * @throws UnsupportedEncodingException */ public static String getIdCardDetail(String cardNo) throws UnsupportedEncodingException{ // 获取身份证信息 IdCard idcard = getIdCardInfo(cardNo); // 存储文本信息 StringBuffer news = new StringBuffer(); if (idcard != null) { news.append("所属地区:"+idcard.getAtt()).append("\n"); news.append("出生日期:"+idcard.getBorn()).append("\n"); news.append("性别:"+idcard.getSex()).append("\n"); } if(news.length() == 0){ news.append("身份证号码").append(cardNo).append("不存在,请重新输入!"); } return news.toString(); } /** * 获取身份证信息 * @param cardNo * @return 返回身份证信息 */ public static IdCard getIdCardInfo(String cardNo){ URL url = null; IdCard idCard = new IdCard(); try{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); url = new URL("http://api.k780.com:88/?app=idcard.get&idcard="+cardNo+"&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=xml"); System.out.println(url); Document doc = builder.parse(url.openStream()); NodeList node = doc.getElementsByTagName("result"); for(int i=0;i<node.getLength();i++){ String idcard = ""; String born = ""; String sex = ""; String att = ""; if(doc.getElementsByTagName("idcard").item(i).getFirstChild() != null){ idcard = doc.getElementsByTagName("idcard").item(i).getFirstChild().getNodeValue(); } if(doc.getElementsByTagName("born").item(i).getFirstChild() != null){ born = doc.getElementsByTagName("born").item(i).getFirstChild().getNodeValue(); } if(doc.getElementsByTagName("sex").item(i).getFirstChild() != null){ sex = doc.getElementsByTagName("sex").item(i).getFirstChild().getNodeValue(); } if(doc.getElementsByTagName("att").item(i).getFirstChild() != null){ att = doc.getElementsByTagName("att").item(i).getFirstChild().getNodeValue(); } idCard.setIdCard(idcard); idCard.setBorn(born); idCard.setSex(sex); idCard.setAtt(att); } }catch(Exception e){ e.printStackTrace(); } return idCard; } public static void main(String[] args){ try { System.out.print(getIdCardDetail("110101199001011118")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
返回结果:
所属地区:北京市东城区
出生日期:1990年01月01日
性别:男
原文:http://blog.csdn.net/wangxw8746/article/details/37905213