1 import java.io.IOException; 2 import java.io.StringWriter; 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Map.Entry; 9 import org.dom4j.Document; 10 import org.dom4j.DocumentHelper; 11 import org.dom4j.Element; 12 import org.dom4j.io.OutputFormat; 13 import org.dom4j.io.XMLWriter; 14 15 public class XMLUtilTest { 16 17 /** 18 * @param args 19 * @throws Exception 20 */ 21 public static void main(String[] args) throws Exception { 22 XMLUtilTest a = new XMLUtilTest(); 23 Map map = new HashMap(); 24 String a1 = a.mapToXml_ygyy(map, "msg"); 25 //给msg节点 添加一个新的 节点 head 26 map = new HashMap(); 27 map.put("VER", "1.0"); 28 map.put("YLJGDM", "1001"); 29 String a2 = a.mapToXml_ygyy(map, "head"); 30 a1 = a.addNode(a1, a2) ; 31 // 获取 体征信息 32 map = new HashMap(); 33 map.put("SG", "170"); 34 map.put("TZ", "60"); 35 String tzxx = a.mapToXml_ygyy(map, "TZXX") ; //体征 36 List<Map> list = new ArrayList<Map>(); 37 for (int i = 0; i < 3; i++) { 38 map = new HashMap(); 39 map.put("ZDXH", "1"); 40 map.put("JZZDBM", "152w"); 41 map.put("JZZDSM", "上呼吸道感染"); 42 list.add(map); 43 } 44 String zdxx = a.ListToXml_ygyy(list, "zd"); //诊断 45 map = new HashMap(); 46 map.put("zs", "患者向医师描述的对自身本次疾病相关的感受的主要记录最大长度500字节"); 47 map.put("ZZMS", "生病了"); 48 String body = a.mapToXml_ygyy(map, "body"); 49 50 map = new HashMap(); 51 map.put("YZID", "11"); 52 map.put("YYXMMC", "氯化钠"); 53 String YP = a.mapToXml_ygyy(map, "YP"); 54 String FYP = a.mapToXml_ygyy(map, "FYP"); 55 String PRESCRIPTION = a.mapToXml_ygyy(new HashMap(), "PRESCRIPTION"); 56 PRESCRIPTION = a.addNode(PRESCRIPTION, YP); 57 PRESCRIPTION = a.addNode(PRESCRIPTION, FYP); 58 59 map = new HashMap(); 60 map.put("YYKSMC", "内科"); 61 body = a.mapToXml_ygyy(map, "body"); 62 body = a.addNode1(body, PRESCRIPTION); 63 64 a1 = a.addNode1(a1, body); //添加节点body到 msg节点是时候用 addNode1方法 65 System.out.println(a1); 66 } 67 68 /** 69 * 组装xml 字符串 70 * @param map 数据对象 map 71 * @param jycode 交易码 72 * @throws IOException 73 */ 74 public String mapToXml(Map map,String jycode) throws Exception{ 75 StringBuffer sb = new StringBuffer(); 76 String userid = "",password = "",trans_no=jycode; 77 sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 78 sb.append("<body><head><userid>"+userid+"</userid><password>"+password+"</password><trans_no>"+trans_no+"</trans_no></head>"); 79 sb.append("<resquest><"+jycode+">"); 80 Iterator<Entry<String, String>> it = map.entrySet().iterator(); 81 while (it.hasNext()) { 82 Map.Entry<String, String> entry = it.next(); 83 String key = entry.getKey().toUpperCase(); 84 String value = entry.getValue(); 85 sb.append("<"+key+">"); 86 sb.append(value); 87 sb.append("</"+key+">"); 88 } 89 sb.append("</"+jycode+"></resquest></body>"); 90 return sb.toString(); 91 } 92 /** 93 * 解析接口返回字符串 返回map对象 94 * @param xml 中医馆接口 返回的xml 字符串 95 * @return 96 * @throws Exception 97 */ 98 public Map xmlTomap(String xml) throws Exception{ 99 Map map = new HashMap(); 100 Document document = DocumentHelper.parseText(xml); 101 Element root = document.getRootElement(); // 根节点body 102 Element response=root.element("response"); 103 Element ret_code=response.element("ret_code"); 104 Element ret_info=response.element("ret_info"); 105 String code = ret_code.getText().trim(); 106 String codeName = ret_code.getName(); 107 String content = ret_info.getText().trim(); 108 String contentName = ret_info.getName(); 109 map.put(codeName, code); 110 map.put(contentName, content); 111 return map; 112 } 115 117 /** 118 * 传入map 对象 返回 xml 节点 119 * @param map 120 * @param jycode 121 * @return 122 * @throws Exception 123 */ 124 public String mapToXml_ygyy(Map map,String codename) throws Exception{ 125 //map 为null 的时候返回空节点 126 codename= codename.toUpperCase(); 127 StringBuffer sb = new StringBuffer(); 128 sb.append("<"+codename+">"); 129 Iterator<Entry<String, String>> it = map.entrySet().iterator(); 130 while (it.hasNext()) { 131 Map.Entry<String, String> entry = it.next(); 132 String key = entry.getKey().toUpperCase(); 133 String value = entry.getValue(); 134 sb.append("<"+key+">"); 135 sb.append(value); 136 sb.append("</"+key+">"); 137 } 138 sb.append("</"+codename+">"); 139 return sb.toString(); 140 } 141 /** 142 * 传入list 对象 返回 xml 节点 (会返回多个同级节点) 143 * @param list list对象 144 * @param codename 节点名称 145 * @return 146 * @throws Exception 147 */ 148 public String ListToXml_ygyy(List<Map> list, String codename) throws Exception{ 149 codename = codename.toUpperCase(); 150 if(list.size()==0){ 151 return ""; 152 } 153 StringBuffer sb = new StringBuffer(); 154 for (Map map : list) { 155 sb.append("<"+codename+">"); 156 Iterator<Entry<String, String>> it = map.entrySet().iterator(); 157 while (it.hasNext()) { 158 Map.Entry<String, String> entry = it.next(); 159 String key = entry.getKey().toUpperCase(); 160 String value = entry.getValue(); 161 sb.append("<"+key+">"); 162 sb.append(value); 163 sb.append("</"+key+">"); 164 } 165 sb.append("</"+codename+">"); 166 } 167 return sb.toString(); 168 } 169 /** 170 * 添加子节点到 原来的节点里面 171 * @param nowNode 目前的节点 172 * @param addNode 要添加的节点 173 * @throws Exception 174 */ 175 public String addNode(String nowNode,String addNode) throws Exception{ 176 if("".equals(addNode) || "".equals(nowNode)){ 177 return nowNode; 178 } 179 addNode = "<root>"+addNode+"</root>"; 180 Document nowNode_doc = DocumentHelper.parseText(nowNode); 181 Document addNode_doc = DocumentHelper.parseText(addNode); //要添加的xml的文档对象 182 Element a1 = nowNode_doc.getRootElement(); 183 Element a2 = addNode_doc.getRootElement();//要添加的xml的文档对象 184 185 List<Element> listElement=a2.elements();//所有一级子节点的list 186 for(Element e:listElement){//遍历所有一级子节点 187 Element cloned = (Element) e.clone (); 188 a1.add(cloned); 189 } 190 StringWriter sw = new StringWriter(); 191 XMLWriter writer = null; 192 OutputFormat format = OutputFormat.createPrettyPrint(); 193 format.setEncoding("UTF-8"); // 设置XML文档的编码类型 194 format.setIndent(true); // 设置是否缩进 195 format.setIndent(" "); // 以空格方式实现缩进 196 format.setNewlines(true); // 设置是否换行 197 try { 198 writer = new XMLWriter(format); 199 writer.setWriter(sw); 200 writer.write(nowNode_doc); 201 writer.close(); 202 } catch (IOException e) { 203 e.printStackTrace(); 204 } 205 return sw.toString(); 206 } 207 public String addNode1(String nowNode,String addNode) throws Exception{ 208 if("".equals(addNode) || "".equals(nowNode)){ 209 return nowNode; 210 } 211 Document nowNode_doc = DocumentHelper.parseText(nowNode); 212 Document addNode_doc = DocumentHelper.parseText(addNode); //要添加的xml的文档对象 213 Element a1 = nowNode_doc.getRootElement(); 214 Element a2 = addNode_doc.getRootElement();//要添加的xml的文档对象 215 a1.add(a2); 216 StringWriter sw = new StringWriter(); 217 XMLWriter writer = null; 218 OutputFormat format = OutputFormat.createPrettyPrint(); 219 format.setEncoding("UTF-8"); // 设置XML文档的编码类型 220 format.setIndent(true); // 设置是否缩进 221 format.setIndent(" "); // 以空格方式实现缩进 222 format.setNewlines(true); // 设置是否换行 223 try { 224 writer = new XMLWriter(format); 225 writer.setWriter(sw); 226 writer.write(nowNode_doc); 227 writer.close(); 228 } catch (IOException e) { 229 e.printStackTrace(); 230 } 231 return sw.toString(); 232 } 233 }
时间: 2024-10-27 12:53:57