package com.szkingdom.leejun; import java.io.InputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; /** * 这里是项目中的简易代码目的是为了解析特有格式的xml * @author leejun * */ public class DecodeXML { //InputStream strm_xml = new FileInputStream("F:\stud.xml"); public static TreeMap<String, String> map = new TreeMap<String,String>(); public static String MD5_CODE; public static void getTreeMap(InputStream strm_xml) { try { SAXReader reader = new SAXReader(); reader.setEncoding("GBK"); //设置打开编码 Document document = reader.read(strm_xml); //生成document对象 Element rootElm = document.getRootElement(); //获取到根节点Message //迭代取出Message的子节点存入有序的map集合中,并且将md5签名存入成员变量中 for(Iterator it = rootElm.elementIterator();it.hasNext();){ Element subElm = (Element) it.next(); String tmpKey = subElm.getName(); String tmpValue = subElm.getText(); System.out.println("key:"+subElm.getName()+"-----------value:"+subElm.getText()); if("sign".equals(tmpKey)) { MD5_CODE = tmpValue; } else { map.put(tmpKey, tmpValue); } } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //验签程序 public static boolean signValid() { //生成指定格式的字符串 if(map.isEmpty()) return false; StringBuilder sbMD5 = new StringBuilder(); Set<String> keys = map.keySet(); String tmpMD5 = ""; for(String tmpKey : keys) { String tmpValue = map.get(tmpKey); sbMD5.append(tmpKey+"="+tmpValue+"&"); } sbMD5.deleteCharAt(sbMD5.length()-1); System.out.println(sbMD5); //将字符串加密 try { MessageDigest mdInst = MessageDigest.getInstance("MD5"); mdInst.update(sbMD5.toString().getBytes()); tmpMD5 = mdInst.digest().toString(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(tmpMD5); if(MD5_CODE.equals(tmpMD5)) { return true; } else { return false; } } }