恩~是可行,但代码方面如何?用JDOM? 如果xml比较复杂?

解决方案 »

  1.   

    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();
         }
    }
      

  2.   

    我想知道怎么对应任何xml文件的转入数据库,我这里代码只是一个制定格式的xml文件各位老大教一下我啦~~好急
      

  3.   

    如果格式不确定,或者说你根本不知道XML文件会有什么内容,我感觉是没有办法解析的。如果是导入数据库内容,至少应该知道表名、字段名、字段类型、数据库表记录
      

  4.   

    比如说,
    <table name="ABC">
      <column name="A" type="char" length=""/>
      <rows>
        <row id="" A="" B=""/>
      </rows>
    </table>
      

  5.   

    我修改过但现在还有报错 空的指针?? 不知道怎么解决~~
    应该怎么修改?成功连接数据库: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= "ppd";
        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();
         }
    }
      

  6.   

    dom4j或者xmlbean。
    不确定的文件没啥好办法。
      

  7.   

    真的要帮忙下编译成功了~但不能插入到数据库急啊 ~~~大哥
    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= "ppd";
        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) {
            //*********************************************************************
    stmt =con.createStatement();   
            //**********************读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 sql0 ="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(sql0);
      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();
         }
    }