读取xml文件信息并存入数据库


controller.java

@RequestMapping("/readXMLFile") public String readXMLFile(HttpServletRequest request,HttpServletResponse response) {SAXReader saxReader=new SAXReader(); org.dom4j.Document document; String xmlStr=""; String stauts="成功!"; try {document = saxReader.read(new File("C:\\Users\\zhang\\Desktop\\test.xml")); xmlStr = document.asXML(); //将xml内容转化为字符串demoservice.readXMLFile(xmlStr); System.out.println("信息导入!!"); } catch (Exception e) {stauts="失败!"; xmlStr=""; e.printStackTrace(); }return stauts; }


service.java
@Override public void readXMLFile(String xmlStr) {int count = 0; int index = 0; String str = ""; //Map map = new HashMap(); //LinkedHashSet setlist = new LinkedHashSet(); while(xmlStr.indexOf(str)!=-1){ index=xmlStr.indexOf(str) + str.length(); xmlStr = xmlStr.substring(index); List list = new ArrayList(); UserInfo info =new UserInfo(); //info.setPat_no(xmlStr.substring(xmlStr.indexOf("")+9,xmlStr.indexOf(""))); info.setBed_no(xmlStr.substring(xmlStr.indexOf("")+8,xmlStr.indexOf(""))); info.setInpatient_no(Integer.parseInt(xmlStr.substring(xmlStr.indexOf("")+14,xmlStr.indexOf("")))); info.setAdmiss_times(Integer.parseInt(xmlStr.substring(xmlStr.indexOf("")+14,xmlStr.indexOf("")))); info.setPatient_id(Integer.parseInt(xmlStr.substring(xmlStr.indexOf("")+12,xmlStr.indexOf("")))); info.setName(xmlStr.substring(xmlStr.indexOf("")+6,xmlStr.indexOf(""))); info.setSex(xmlStr.substring(xmlStr.indexOf("")+5,xmlStr.indexOf(""))); info.setBirthday(xmlStr.substring(xmlStr.indexOf("")+10,xmlStr.indexOf(""))); info.setAge(Integer.parseInt(xmlStr.substring(xmlStr.indexOf("")+5,xmlStr.indexOf("")))); info.setAge_unit(xmlStr.substring(xmlStr.indexOf("")+10,xmlStr.indexOf(""))); info.setHuli(xmlStr.substring(xmlStr.indexOf("")+6,xmlStr.indexOf(""))); info.setYinshi(xmlStr.substring(xmlStr.indexOf("")+8,xmlStr.indexOf(""))); info.setIcd(xmlStr.substring(xmlStr.indexOf("")+5,xmlStr.indexOf(""))); info.setAdmiss_date(xmlStr.substring(xmlStr.indexOf("")+13,xmlStr.indexOf(""))); info.setConsult_physician(Integer.parseInt(xmlStr.substring(xmlStr.indexOf("")+19,xmlStr.indexOf("")))); info.setConsult_physicia_name(xmlStr.substring(xmlStr.indexOf("")+23,xmlStr.indexOf(""))); info.setNurse_code(xmlStr.substring(xmlStr.indexOf("")+12,xmlStr.indexOf(""))); info.setNurse(xmlStr.substring(xmlStr.indexOf("")+7,xmlStr.indexOf(""))); info.setInout(xmlStr.substring(xmlStr.indexOf("")+7,xmlStr.indexOf(""))); info.setDept(xmlStr.substring(xmlStr.indexOf("")+6,xmlStr.indexOf(""))); info.setSocial_code(xmlStr.substring(xmlStr.indexOf("")+13,xmlStr.indexOf(""))); info.setResponse(xmlStr.substring(xmlStr.indexOf("")+10,xmlStr.indexOf(""))); info.setInsur_code(xmlStr.substring(xmlStr.indexOf("")+12,xmlStr.indexOf(""))); info.setHome_tel(xmlStr.substring(xmlStr.indexOf("")+10,xmlStr.indexOf(""))); info.setHome_street(xmlStr.substring(xmlStr.indexOf("")+13,xmlStr.indexOf(""))); info.setFycpf(xmlStr.substring(xmlStr.indexOf("")+7,xmlStr.indexOf(""))); info.setAllergy(xmlStr.substring(xmlStr.indexOf("")+9,xmlStr.indexOf(""))); // //info.setXyflag(xmlStr.substring(xmlStr.indexOf("")+8,xmlStr.indexOf(""))); count++; list.add(info); try {usermapper.insetr(list); } catch (Exception e) { e.printStackTrace(); } }


dao.java
public interface UserMapper { public void insetr(@Param("list") List list); }


mapper.xml
insert into V_ykz_zyhzctk ( BED_NO, INPATIENT_NO, ADMISS_TIMES, PATIENT_ID, NAME, SEX, BIRTHDAY, AGE, AGE_UNIT, HULI, YINSHI, ICD, ADMISS_DATE, CONSULT_PHYSICIAN, CONSULT_PHYSICIA_NAME, NURSE_CODE, NURSE, A_INOUT, DEPT, SOCIAL_CODE, RESPONSE, INSUR_CODE, HOME_TEL, HOME_STREET, FDDPF, ALLERGY )values #{param.bed_no},#{param.inpatient_no,jdbcType=VARCHAR} ,#{param.admiss_times}, #{param.patient_id},#{param.name},#{param.sex}, #{param.birthday},#{param.age},#{param.age_unit}, #{param.huli},#{param.yinshi},#{param.icd}, #{param.admiss_date},#{param.consult_physician},#{param.consult_physicia_name}, #{param.nurse_code},#{param.nurse},#{param.inout}, #{param.dept},#{param.social_code},#{param.response}, #{param.insur_code},#{param.home_tel},#{param.home_street}, #{param.fycpf},#{param.Allergy}

【读取xml文件信息并存入数据库】

    推荐阅读