`
夕阳桥断
  • 浏览: 4068 次
社区版块
存档分类
最新评论

服务器返回的字符串xml解析验证是不是真实存在此证件

    博客分类:
  • java
阅读更多

/**
 *
 *
 * @项目名称:javahtml
 * @类名称:TestMyself
 * @类描述: 验证身份证的相关信息 是不是存在此身份证件
 * @创建人:yangcb
 * @创建时间:2013-9-23 下午2:52:44
 * @version
 *
 */
public class TestMyself {

    /**
     * @Title: main
     * @方法描述: 从服务器返回xml数据格式的字符串
     * @创建人:yangcb
     * @创建时间:2013-9-23 上午10:24:46
     * @返回值: void
     * @version
     * @throws IOException
     *
     */

    public static String testmyself(String number) throws IOException {
        String strURL = "http://www.yodao.com/smartresult-xml/search.s?type=id&q="
                + number;
        URL url = new URL(strURL);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        String cureeentLineString = "";
        String content = "";
        InputStream inputStream = connection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(inputStream));
        while ((cureeentLineString = bufferedReader.readLine()) != null) {
            content += cureeentLineString + "\r\n";
        }
        System.out.println(content);
        return content;
    }

    /**
     *
     * @Title: xmlElement
     * @方法描述:解析字符串形式的xml文件
     * @创建人:yangcb
     * @创建时间:2013-9-23 下午2:48:16
     * @返回值: List
     * @version
     *
     */
    public static List xmlElement(String xmlDoc) {

        try {
            StringReader read = new StringReader(xmlDoc);// 创建新的字符串
            InputSource source = new InputSource(read); // 创建新的输入源SAX 解析器将使用
                                                        // InputSource 对象来确定如何读取
                                                        // XML 输入
            SAXBuilder sb = new SAXBuilder();// 创建一个新的SAXBulider
            org.jdom.Document doc = sb.build(source);// 通过输入源构造一个document
            Element root = doc.getRootElement();// 取出根元素
            // System.out.println(root.getName());//输入根源素名称
            List jiedian = root.getChildren();// 得到根源素的所有子元素集合
            Namespace ns = root.getNamespace();
            Element et = null;
            StringBuilder sb1 = new StringBuilder();
            for (int i = 0; jiedian != null && i < jiedian.size(); i++) {
                et = (Element) jiedian.get(i);// 依次得到子元素
                // System.out.println(et.getChild("code",ns).getText());
                // System.out.println(et.getChild("location",ns).getText());
                List zjiedian = et.getChildren();
                for (int j = 0; zjiedian != null && j < zjiedian.size(); j++) {
                    Element xet = (Element) zjiedian.get(j);
                    // System.out.println(xet.getName());//节点名称
                    // System.out.println("身份证号:"+et.getChildText(xet.getName()));
                    if (j > 0 && j < zjiedian.size()) {
                        sb1.append("-");
                    }
                    sb1.append(et.getChildText(xet.getName()));
                }
            }
            String idMesaage = sb1.toString();
            String[] IDMessage = idMesaage.split("-");
                System.out.println("身份证号:" + IDMessage[0]);
                System.out.println("家庭住址:" + IDMessage[1]);
                System.out.println("出生日期:" + IDMessage[2]);
                System.out.println("性别:"
                        + (IDMessage[3].equals("f") ? "女" : "男"));
        } catch (JDOMException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws IOException {

        String content = testmyself("1222222222");// 参数为身份证号
        System.out.println(content.length());//不存在返回字符串的长度
        if(content.length()==54){//不存在返回字符串的长度
            System.out.println("不存在此人的信息");
        }else{
            xmlElement(content);
        }
       
    }

}

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics