0
回答
求大神解析这段 xml 然后insert进去
百度AI开发者大赛带你边学边开发,赢100万奖金,加群:418589053   
<?xml version="1.0" encoding="utf-8"?>
<root>
  <Sentences>
    <DeptKind>
      <name>1</name>
      <PartKind>
        <name>1.1</name>
        <SickKind>
          <name>1.1.1</name>
          <Sentence>
            <Report><![CDATA[    1.1.1.1内容]]></Report>
            <Conclusion><![CDATA[1.1.1.2内容]]></Conclusion>
          </Sentence>
        </SickKind>      
      </PartKind>      
    </DeptKind>
    <DeptKind> 
      <name>2</name>
      <PartKind>
        <name>2.1</name>
        <SickKind>
          <name>2.1.1</name>
          <Sentence>
            <Report><![CDATA[    2.1.1.1内容]]></Report>
            <Conclusion><![CDATA[2.1.1.2内容]]></Conclusion>
          </Sentence>
        </SickKind>      
      </PartKind>     
    </DeptKind>
  </Sentences>
</root>

这是xml.

用java解析 插入mysql数据库

 

public static void main(String[] args) throws Exception{
            //插入数据的sql语句
            String sql="insert into template_tab(name, name2, name, Report,Conclusion) values (?, ?, ?, ?, ?)";
            Connection conn=null;
            PreparedStatement pstmt=null;           
            try{                
                 Class.forName("com.mysql.jdbc.Driver");
                   System.out.println("测试通过");
                    conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1/pacs","root","root");
 
                 
                pstmt=conn.prepareStatement(sql);
                //读取xml文件
                Document doc=new SAXReader().read(new File("D:/template2.xml"));
                //选择xml文件的节点
                List itemList=doc.selectNodes("root/Sentences/DeptKind");
                //遍历读出的xml中的节点
                for(Iterator iter=itemList.iterator();iter.hasNext();){
                    Element el=(Element)iter.next();
                    //读取节点内容
                    String name=el.elementText("name");
 
                    List PartKind = el.elements("PartKind");
                    StringBuffer sbString=new StringBuffer();
                 
                        //Element turnosElt=(Element)iter.next();
                        String name2 = ((Element) PartKind).elementText("name");
 
                    List SickKind = el.elements("SickKind");
     
                        Element turnosElt2=(Element)iter.next();
                        String name3 = turnosElt2.elementText("name");                      
                        //sbString.append(name3/* + "," + ma + "," + mi + "," + ju + "," + vi + "," + sa + "," + doo*/);
                    //}
                    List Sentence = el.elements("Sentence");
                    for(Iterator iter1=Sentence.iterator();iter1.hasNext();){                   
                        Element turnosElt3=(Element)iter1.next();
                        String Report = turnosElt3.elementText("Report");
                        //String Conclusion = PartKind.elementText("Conclusion");
                        //sbString.append(Report + "," + Conclusion );
                    }
                     
                     
                    //为sql语句赋值
                    pstmt.setString(1, name);
                    pstmt.setString(2, PartKind.toString());
                    pstmt.setString(3, SickKind.toString());
                    pstmt.setString(4, Sentence.toString());
                    pstmt.setString(5,sbString.toString());
                    pstmt.addBatch();
                }
                pstmt.executeBatch();
                System.out.print("将XML导入数据库成功");
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                //DbUtil.close(pstmt);
                //.close(conn);
                //DbUtil.CloseDB();
                //DbUtil.OpenDB();
                conn.close();
            }
        }

这是自己弄了下  各种问题  取不到节点... 请大神帮忙!!

 


举报
看了看
发帖于3年前 0回/143阅
顶部