代码如下,恳求各位大哥指教
import org.jdom.Document; 
import org.jdom.Element; 
import org.jdom.input.SAXBuilder; 
import org.jdom.output.Format; 
import org.jdom.output.XMLOutputter; 
import java.sql.*; 
import java.util.*; public class ReadXml  

     public  Connection conn = null; 
 public  Statement stmt = null ; 
  
 public void readXml() 
 { 
 //***************************建立数据库连接*************************** 
 try 
 { 
Class.forName("org.gjt.mm.mysql.Driver").newInstance();  
String url ="jdbc:mysql://localhost/jdomdb?user=root&password=";//jdomdb为你的数据库名  
   conn = DriverManager.getConnection(url);  
   stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  
 } 
 catch(Exception sqlexception) 
 { 
System.out.println("数据库连接发生异常!"); 
 } 
        //*********************************************************************         //**********************读xml文件************************************** 
try 
{  
  SAXBuilder sb = new SAXBuilder();   Document doc = sb.build("userinfo.xml"); 
  Element root = doc.getRootElement();           String username = "",id="" ,homephone="",officephone=""; 
  String password ="",usernum="" ; 
  String homeaddress ="",officeaddress = "",mobile="" ; 
   
  Element elms = null; 
  Element elms2 = null; 
  Element elms3 = null;   List list2 = null; 
  List list3 = null; 
  List list1 = root.getChildren("userinfo"); 
           
  for(int i=0; i < list1.size(); i++) 
  {  
  elms = (Element)list1.get(i);        //userinfo节点子元素   id = elms.getChildText("id"); 
  usernum = elms.getChildText("usernum"); 
              username = elms.getChildText("username"); 
  password = elms.getChildText("password"); 
          list2 = elms.getChildren("userphone");   //读出userinfo节点的userphone子节点的元素 
   
  for(int k=0; k < list2.size(); k++) 
  {  
  elms2 = (Element)list2.get(k); 
  homephone = elms2.getChildText("homephone"); 
  officephone = elms2.getChildText("officephone"); 
  mobile = elms2.getChildText("mobile"); 
  } 
  list3 = elms.getChildren("useraddress");   //读出useraddress节点的元素      for(int k=0; k < list3.size(); k++) 
  {  
  elms3 = (Element)list3.get(k); 
  homeaddress = elms3.getChildText("homeaddress"); 
  officeaddress = elms3.getChildText("officeaddress"); 
  }   //插入数据库的表tmpinfo 
  String sql = "insert into tmpinfo(usernum,username,password,homephone,officephone,mobile,homeaddress,officeaddress)values ('"+usernum+"','"+username+"','"+password+"','"+homephone+"','"+officephone+"','"+mobile+"','"+homeaddress+"','"+officeaddress+"')"; 
          stmt.executeUpdate(sql);   }//for 
   
  stmt.close();  
  conn.close();  

catch(Exception e) 

e.printStackTrace(); 

    //**********************读xml文件************************************** 
 } 
 public static void main(String[] args) 
 { 
          ReadXml rx = new ReadXml(); 
          rx.readXml(); 
     } 

解决方案 »

  1.   

    晕~~
    我想知道怎么对应任何xml文件的转入数据库,我这里代码只是一个制定格式的xml文件
      

  2.   

    没有统一格式的东西,像你上面的应该说是一个xml文件对应一个数据库的表。作出一个共同的东西不可能的。
      

  3.   

    我修改过但现在还有报错 空的指针?? 不知道怎么解决~~ 
    应该怎么修改? 成功连接数据库:xml 
    java.lang.NullPointerException 
    at XMLViewGenerator.xml.xmltomysql(xml.java:82) 
    at XMLViewGenerator.xml.main(xml.java:98) 
    package XMLViewGenerator;import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.input.SAXBuilder;
    import java.sql.*;
    import java.util.*;public class xml 
    {
            public  Connection con = null;
            public  Statement stmt = null ;
            private final String url = "jdbc:mysql://localhost:3306/test";
            private final String databaseName= "xml";
            private final String userName = "root";
            private final String password = "123456";
          private java.sql.Connection getConnection() {
                try{ Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection(url,userName,password);
                if(con!=null)
                    System.out.println("成功连接数据库:"+databaseName); } catch(Exception e) {
                        e.printStackTrace();
                        System.out.println("跟踪在方法getConnection()出现的错误 : " + e.getMessage()); }
                return con; }        private void closeConnection() {
                try {
                    if(con!=null)
                        con.close();
                    con=null; } catch(Exception e){ e.printStackTrace(); } }     
         public void xmltomysql()
         {
             //***************************建立数据库连接***************************
             try{con= this.getConnection();
             if(con!=null) {
            //*********************************************************************        //**********************读xml文件**************************************
            try
            { 
              SAXBuilder sb = new SAXBuilder();          Document doc = sb.build("xml.xml");
              Element root = doc.getRootElement();          String id="";
              String name="";
              String game="";
              String score="";
              
              Element elms = null;
              Element elms2 = null;
              List list2 = null;
              List list3 = null;
              List list1 = root.getChildren("xml");//得到根节点的所有子节点,返回的是List类型的变量
              
              for(int i=0; i< list1.size(); i++)
              { 
                  elms = (Element)list1.get(i);        //userinfo节点子元素              id = elms.getChildText("id");
                  name = elms.getChildText("name");
                  
                  list2 = elms.getChildren("game");   //读出userinfo节点的userphone子节点的元素
                  
                  for(int k=0; k< list2.size(); k++)
                  { 
                      elms2 = (Element)list2.get(k);
                      score = elms2.getChildText("score");
                  }              //插入数据库的表info
                  String sql1 ="CREATE TABLE info (id INT,name CHAR,game CHAR, score INT);";
                  String sql = "insert into info(id,name,game,score)values ('"+id+"','"+name+"','"+game+"','"+score+"')";
                  stmt.executeUpdate(sql1);
                  stmt.executeUpdate(sql);          }//for
              
              stmt.close();
              closeConnection(); 
            }
            
            catch(Exception e)
            {e.printStackTrace();}
            //**********************读xml文件**************************************
        }
             else
             System.out.println("错误: 没有可用的数据库连接!");} catch(Exception e){ e.printStackTrace(); }
            }
         public static void main(String[] args)throws Exception
         {
              xml rx = new xml();
              rx.xmltomysql();
         }
    }