java 解析xml 并导入数据库(dom4j )
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
java 解析xml 并导入数据库(dom4j ) import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class TestXMLImport {
/**
* @param args
*/
public static void main(String[] args) {
String sql = "insert into T_XML(NUMERO, REPOSICION, NOMBRE, TURNOS) values (?, ?, ?, ?)";
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DbUtil.getConnection();
pstmt = conn.prepareStatement(sql);
Document doc = new SAXReader().read(new
File("D:/share/JavaProjects/drp/test_xmlImport/xml/test01.XML "));
List itemList = doc.selectNodes("/ACCESOS/item/SOCIO");
for (Iterator iter=itemList.iterator(); iter.hasNext();) { Element el = (Element)iter.next();
String numero = el.elementText("NUMERO");
String reposicion = el.elementText("REPOSICION");
String nombre = el.elementText("NOMBRE");
List turnosList = el.elements("TURNOS");
StringBuffer sbString = new StringBuffer();
for (Iterator iter1=turnosList.iterator(); iter1.hasNext();) {
Element turnosElt = (Element)iter1.next();
String lu = turnosElt.elementText("LU");
String ma = turnosElt.elementText("MA");
String mi = turnosElt.elementText("MI");
String ju = turnosElt.elementText("JU");
String vi = turnosElt.elementText("VI");
String sa = turnosElt.elementText("SA");
String doo = turnosElt.elementText("DO");
sbString.append(lu + "," + ma + "," + mi + "," + ju + "," + vi + "," + sa + "," + doo);
}
pstmt.setString(1, numero);
pstmt.setString(2, reposicion);
pstmt.setString(3, nombre);
pstmt.setString(4, sbString.toString());
pstmt.addBatch();
}
pstmt.executeBatch();
System.out.println("将XML导入数据库成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
DbUtil.close(pstmt);
DbUtil.close(conn);
}
}
}
---------------------------------------------------------------------------------------------------------------------------------