• dom4j解析xml


    XML文件

    <?xml version="1.0" encoding="UTF-8"?>

    <Request>

        <TransactionId>10242</TransactionId>

        <RBASRec>

           <Dept>高血压门诊</Dept>

           <DeptId>576</DeptId>

           <DoctorId>2513</DoctorId>

           <DoctorName>杨国勋</DoctorName>

           <DoctorTitle>副主任医师</DoctorTitle>

           <RBASDate>2012-01-05</RBASDate>

           <RBASId>1022||92</RBASId>

           <RBASPrice>.5</RBASPrice>

           <RBASSessionCode>上午</RBASSessionCode>

           <StartTime>08:00</StartTime>

           <EndTime>12:00</EndTime>

           <RBASSessionLoad>7</RBASSessionLoad>

           <RBASSessionType>副主任号</RBASSessionType>

           <RBASSessionTypeId>11</RBASSessionTypeId>

           <AdmitAddress>高血压门诊</AdmitAddress>

             <ASTimeRangeFlag>Y</ASTimeRangeFlag>         

            <ASTRRegInfoStr>08:30-09:30^5,09:30-10:30^5,10:30-11:30^5</ASTRRegInfoStr>

        </RBASRec>

    二、解析

    解析之前准备dom4j.jar/官网下载后放在项目lib目录下

    创建ParseXML类

    public class ParseXML {

       public static void main(String[] args) {

         try {

    String xml=’拼接xml字段’

             Document doc = DocumentHelper.parseText(xml);

             Element root = doc.getRootElement();

             //停医生

             Iterator itersElIterator = root.elementIterator("RBASRec");

             while (itersElIterator.hasNext()) {

                Element RBASRecNew = (Element) itersElIterator.next();

                String Dept = RBASRecNew.elementTextTrim("Dept");

                String DeptId = RBASRecNew.elementTextTrim("DeptId");

                String DoctorId = RBASRecNew.elementTextTrim("DoctorId");

                String DoctorName = RBASRecNew.elementTextTrim("DoctorName");

                String DoctorTitle = RBASRecNew.elementTextTrim("DoctorTitle");

                String RBASDate = RBASRecNew.elementTextTrim("RBASDate");

                String RBASId = RBASRecNew.elementTextTrim("RBASId");

                String RBASPrice = RBASRecNew.elementTextTrim("RBASPrice");

                String RBASSessionCode = RBASRecNew.elementTextTrim("RBASSessionCode");

                String StartTime = RBASRecNew.elementTextTrim("StartTime");

                String EndTime = RBASRecNew.elementTextTrim("EndTime");

                String RBASSessionLoad = RBASRecNew.elementTextTrim("RBASSessionLoad");

                String RBASSessionType = RBASRecNew.elementTextTrim("RBASSessionType");

                String RBASSessionTypeId = RBASRecNew.elementTextTrim("RBASSessionTypeId");

                String AdmitAddress = RBASRecNew.elementTextTrim("AdmitAddress");

                String ASTimeRangeFlag = RBASRecNew.elementTextTrim("ASTimeRangeFlag");

                String ASTRRegInfoStr = RBASRecNew.elementTextTrim("ASTRRegInfoStr");

                //把遍历的节点存放在Map中

                Map<String,String> stMap = new HashMap<String,String>();

                stMap.put("Id", UUID.randomUUID().toString());

                stMap.put("Dept", Dept);

                stMap.put("DeptId", DeptId);

                stMap.put("DoctorId", DoctorId);

                stMap.put("DoctorName", DoctorName);

                stMap.put("DoctorTitle", DoctorTitle);

                stMap.put("RBASDate", RBASDate);

                stMap.put("RBASId", RBASId);

                stMap.put("RBASPrice", RBASPrice);

                stMap.put("RBASSessionCode", RBASSessionCode);

                stMap.put("StartTime", StartTime);

                stMap.put("EndTime", EndTime);

                stMap.put("RBASSessionLoad", RBASSessionLoad);

                stMap.put("RBASSessionType", RBASSessionType);

                stMap.put("RBASSessionTypeId", RBASSessionTypeId);

                stMap.put("AdmitAddress", AdmitAddress);

                stMap.put("ASTimeRangeFlag", ASTimeRangeFlag);

                stMap.put("ASTRRegInfoStr", ASTRRegInfoStr);

         //每个map存放一个医生,再放入集合list里

                List<Map<String,String>> stList=new ArrayList<Map<String,String>>();

                stList.add(stMap);

                InsertXmlist.linegc(stList); 

             }

    } catch (DocumentException e) {

             e.printStackTrace();

          }

       }

    }

    三、创建InsertXmlist类遍历入库

    public static String linegc(List<Map<String,String>> xmllist) {

          Connection conndyzd = null;

          PreparedStatement psyzd = null;

          List<Map<String,String>> pays=xmllist;

          try {

             //阿里云durid

             conndyzd=DruidDatasource.getConnection();

             conndyzd.setAutoCommit(false);

             psyzd = conndyzd.prepareStatement(sql语句);

      for (Map<String,String> pay: pays) { 

                psyzd.setString(1, pay.get("Dept"));

                psyzd.setString(2, pay.get("DeptId"));

                psyzd.setString(3, pay.get("DoctorId"));

                psyzd.setString(4, pay.get("DoctorName"));

                psyzd.setString(5, pay.get("DoctorTitle"));

                psyzd.setString(6, pay.get("RBASDate"));

                psyzd.setString(7, pay.get("RBASId"));

                psyzd.setString(8, pay.get("RBASPrice"));

                psyzd.setString(9, pay.get("RBASSessionCode"));

                psyzd.setString(10, pay.get("StartTime"));

                psyzd.setString(11, pay.get("EndTime"));

                psyzd.setString(12, pay.get("RBASSessionLoad"));

                psyzd.setString(13, pay.get("RBASSessionType"));

                psyzd.setString(14, pay.get("RBASSessionTypeId"));

                psyzd.setString(15, pay.get("AdmitAddress"));

                psyzd.setString(16, pay.get("ASTimeRangeFlag"));

                psyzd.setString(17, pay.get("ASTRRegInfoStr"));

                psyzd.addBatch();//批量操作        

             }

             // 批量执行

             psyzd.executeBatch();

             conndyzd.commit();

          }catch (SQLException e) {

             e.printStackTrace();

             return expUtils(e);

          } finally {

             try {

                if (psyzd != null)

                    psyzd.close();

                if (conndyzd != null)

                    conndyzd.close();

             } catch (SQLException e) {

                e.printStackTrace();

                return expUtils(e);

             }

          }

          return "1";

       }

      

    public static String expUtils(Exception e) {

       String title = "Exception " + e.getMessage();

       StringBuffer msg = new StringBuffer();

       for (StackTraceElement ex : e.getStackTrace()) {

          msg.append("at: " + ex.toString() + " ");

       }

       return title + " " + msg;

      }

    }

    四、创建DataSoures连接库

    public class DataSoures {

       /*

        * 6步 1步 加载(注册)驱动 2步 获取数据库连接对象 Connection 3步 创建执行SQL语句对象 Statement 4步执行SQL语句过程 5步 返回数据结果集对象 ResultSet 6步 关闭资源

        */

       static Properties p = null;

       static {

          // 1步 加载(注册)驱动 

          p = new Properties();

          InputStream is = null;

          try {   

             String driverName = "com.mysql.jdbc.Driver";

             Class.forName(driverName);

          } catch (Exception e) {

     

             e.printStackTrace();

          }

       }

       // -------------------TODO-----------------获取连接

       public static Connection getCon() {

            String url = "jdbc:mysql://localhost:3306/srkl";

            String user = "root";

            String password = "root";

          Connection con = null;

          // 2步 获取数据库连接对象 Connection

          try {

             con = DriverManager.getConnection(url, user, password);

          } catch (SQLException e) {

             e.printStackTrace();

          }

          return con;

       }

       // -----TODO------------ 关闭资源公共方法

          public static void closeAll(Connection con, Statement st, ResultSet rst) {

             try {

                if (rst != null) {

                    rst.close();

                }

                if (st != null) {

                    st.close();

                }

                if (con != null) {

                    con.close();

                }

             } catch (Exception e) {

     

             }

          }

    }

    创建数据库srkl创建表,加入xml所有字段及加入id 和createtime(实时时间)字段。 

    运行main方法就可以在数据库和表中看到数据了。

  • 相关阅读:
    史上最全的网银转账测试分析与设计
    【面试题】你是测试工程师,如何保证软件的质量?
    小白成长建议--小白如何提问
    [感悟]性能测试测什么
    通过一个简单的数据库操作类了解PHP链式操作的实现
    PHP魔术方法小结.md
    谈PHP中信息加密技术
    PHP输入流php://input [转]
    【PHPsocket编程专题(实战篇③)】构建基于socket的HTTP请求类
    从一次面试经历谈PHP的普通传值与引用传值以及unset
  • 原文地址:https://www.cnblogs.com/wuqiaoqun/p/13091452.html
Copyright © 2020-2023  润新知