1 packagehello.entity.jiexi_xml;2
3 importorg.jdom.Document;4 importorg.jdom.Element;5 importorg.jdom.JDOMException;6 importorg.jdom.Namespace;7 importorg.jdom.input.SAXBuilder;8
9 importjava.io.FileInputStream;10 importjava.io.IOException;11 importjava.io.InputStream;12 import java.sql.*;13 importjava.util.List;14 public classDB {15
16 //数据库的连接
17 private static String URL = "jdbc:mysql://localhost:3306/test";18 private static String USERNAME = "root";19 private static String PASSWORD = "root";20
21
22 public staticConnection getConn(){23 Connection conn = null;24 try{25 Class.forName("com.mysql.jdbc.Driver");26 conn =DriverManager.getConnection(URL,USERNAME,PASSWORD);27 } catch(Exception e) {28 e.printStackTrace();29 } returnconn;30 }31
32 public staticStatement createStmt(Connection conn){33 Statement stmt = null;34 try{35 stmt =conn.createStatement();36 } catch(SQLException e) {37 e.printStackTrace();38 }39 returnstmt;40 }41
42 public staticResultSet executeQuery(Statement stmt,String sql){43 ResultSet rs = null;44 try{45 rs =stmt.executeQuery(sql);46 } catch(SQLException e) {47 e.printStackTrace();48 }49 returnrs;50 }51
52 public staticPreparedStatement prepareStmt(Connection conn,String sql){53 PreparedStatement pStmt = null;54 try{55 pStmt =conn.prepareStatement(sql);56 } catch(SQLException e) {57 e.printStackTrace();58 }59 returnpStmt;60 }61
62
63
64 public static voidmain(String[] args) {65 try{66 XmlParse();67 } catch(JDOMException e) {68 e.printStackTrace();69 } catch(IOException e) {70 e.printStackTrace();71 }72 }73
74
75 public static void XmlParse() throwsJDOMException, IOException {76 SAXBuilder builder = newSAXBuilder();77 InputStream file = new FileInputStream("ceshi.xml"); //测试文件的路径
78
79 {80 Document document = builder.build(file);//加载文件
81 Element root = document.getRootElement();//设置根节点
82
83 Namespace ns = root.getNamespace("gml");84 List list = root.getChildren("a",ns);85 for(Element e1:list){86 Namespace ns2 = e1.getNamespace("esri");87 Element e2 = e1.getChild("x",ns2);88 String ID = e2.getAttributeValue("fid");89 System.out.println("ID =" +ID);90
91 Namespace ns3 = e2.getNamespace("esri");92 Element e3 = e2.getChild("OBJECTID",ns3);93
94 Namespace ns4 = e2.getNamespace("gml");95 Element e4 = e2.getChild("b",ns4);96
97 Namespace ns5 = e4.getNamespace("gml");98 Element e5 = e4.getChild("c",ns5);99
100 Namespace ns6 = e5.getNamespace("gml");101 Element e6 = e5.getChild("d",ns6);102
103 Namespace ns7 = e6.getNamespace("gml");104 Element e7 = e6.getChild("e",ns7);105
106 Namespace ns8 = e7.getNamespace("gml");107 Element e8 = e7.getChild("f",ns8);108
109 Namespace ns9 = e8.getNamespace("gml");110 Element e9 = e8.getChild("g",ns9);111
112 Namespace ns10 = e9.getNamespace("gml");113 Element e10 = e9.getChild("coordinates",ns10);114 String POINT1 =e10.getTextTrim();115 System.out.println("POINT1 =" +POINT1);116
117 Namespace ns11 = e2.getNamespace("gml");118 Element e11 = e2.getChild("envelope",ns11);119 String POINT2 =e11.getTextTrim();120 System.out.println("POINT2 =" +POINT2);121
122 Namespace ns12 = e2.getNamespace("esri");123 Element e12 = e2.getChild("a",ns12);124 String NAME =e12.getTextTrim();125 System.out.println("NAME =" +NAME);126
127 Connection conn =DB.getConn();128 String sql = "insert into CESHI(ID,NAME,POINT1,POINT2) values (?,?,?,?)";129 PreparedStatement pStmt =DB.prepareStmt(conn,sql);130 try{131
132 pStmt.setString(1,ID);133 pStmt.setString(2,NAME);134 pStmt.setString(3,POINT1);135 pStmt.setString(4,POINT2);136 pStmt.executeUpdate();137 } catch(SQLException e) {138 e.printStackTrace();139 }140 }141 }142 }143 public static voidclose(Connection con,Statement sm,ResultSet rs){144 try{145 if(con!=null){146 con.close();147 }148 if(sm!=null){149 sm.close();150 }151 if(rs != null){152 rs.close();153 }154 } catch(SQLException e) {155 e.printStackTrace();156 }157 }158 }159
160