xml文件读取到数据库
第一步,导包
c3p0,dom4j,jaxen,mysql-connector
第二步 xml文件,config文件
第三步 javabean
第四步 c3p0的工具类
第五步 读取xml文件 saxreader中的xpath的方式
首先需要map集合添加别名,遍历读取到的文件,
给了liste1580111763df721c6bab76afcde5ef7
第六步,liste1580111763df721c6bab76afcde5ef7给了c3p0的连接数据库的类
第一步,导包
c3p0,dom4j,jaxen,mysql-connector
第二步 xml文件,config文件
xml文件,config文件根据需求具体实现
xsd的约束
<?xml version="1.0" encoding="utf-8"?>
<schema
xmlns="http://www.w3.org/2001/xmlschema"
targetnamespace="xiaoge"
elementformdefault="qualified">
<element name="group">
<complextype>
<sequence maxoccurs="8" minoccurs="1">
<element name="person">
<complextype>
<sequence>
<element name="name" type="string"></element>
<element name="sex" type="string"></element>
<element name="age" type="string"></element>
</sequence>
</complextype>
</element>
</sequence>
<attribute name="id" type="int" use="required"></attribute>
</complextype>
</element>
</schema>
第三步 javabean
根据需求在具体做
第四步 c3p0的工具类
package com.itheima.util;
import java.sql.connection;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
import com.mchange.v2.c3p0.combopooleddatasource;
public class c3p0util {
private static final combopooleddatasource datasource = new combopooleddatasource();
public static connection getconn(){
try {
return datasource.getconnection();
} catch (sqlexception e) {
e.printstacktrace();
}
return null;
}
public static void release(resultset rs, statement stmt, connection conn){
if (rs != null) {
try {
rs.close();
} catch (sqlexception e) {
e.printstacktrace();
}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (sqlexception e) {
e.printstacktrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (sqlexception e) {
e.printstacktrace();
}
conn = null;
}
}
}
第五步 读取xml文件 saxreader中的xpath的方式
首先需要map集合添加别名,遍历读取到的文件,
给了list<javabean>
package com.itwjx.xml;
import java.sql.connection;
import java.sql.preparedstatement;
import java.text.parseexception;
import java.text.simpledateformat;
import java.util.arraylist;
import java.util.date;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import org.dom4j.document;
import org.dom4j.documentexception;
import org.dom4j.element;
import org.dom4j.io.saxreader;
import org.junit.test;
import com.itwjx.entity.xmldomain;
import com.itwjx.util.c3p0util;
/**
* 数据库名称 demo
* 表名userdomain
* 字段: id int
* name varchar
* birthday date
* hobby char
* @author wbh
*
*/
public class wrokxmlans {
@test
public void readxmltodb(){
try {
//读取xml文件数据
list<xmldomain> domains = readxml("src/aaa.xml");
//将数据保存到数据库
savexmldatetodb(domains);
} catch (exception e) {
e.printstacktrace();
}
}
private list<xmldomain> readxml(string path) throws documentexception, parseexception {
saxreader read = new saxreader();
document document = read.read(path);
map<string, string> map = new hashmap<string, string>();
map.put("wbh", "xiaofan");
read.getdocumentfactory().setxpathnamespaceuris(map);
list<element> nodes = document.selectnodes("//wbh:member");
list<xmldomain> domains = new arraylist<xmldomain>();
for (element element : nodes) {
string id = element.attributevalue("no");
string name = element.element("name").gettext();
string birthday = element.element("birthday").gettext();
string hobby = element.element("hobby").gettext();
simpledateformat df = new simpledateformat("yyyy-mm-dd");
date date = df.parse(birthday);
xmldomain domian = new xmldomain(
integer.parseint(id), name,date, hobby);
domains.add(domian);
}
return domains;
}
private void savexmldatetodb(list<xmldomain> domains) {
//
connection conn = null;
preparedstatement ps = null;
try {
conn = c3p0util.getconn();
ps = conn.preparestatement("insert into userdomain values(?,?,?,?)");
for (xmldomain user : domains) {
ps.setint(1, user.getid());
ps.setstring(2, user.getname());
ps.setdate(3, new java.sql.date(user.getbirthday().gettime()));
ps.setstring(4, user.gethobby());
ps.addbatch();
}
ps.executebatch();
} catch (exception e) {
e.printstacktrace();
} finally {
c3p0util.release(null, ps, conn);
}
}
}
第六步,list<javabean>给了c3p0的连接数据库的类
private void savexmldatetodb(list<xmldomain> domains) {
//
connection conn = null;
preparedstatement ps = null;
try {
conn = c3p0util.getconn();
ps = conn.preparestatement("insert into userdomain values(?,?,?,?)");
for (xmldomain user : domains) {
ps.setint(1, user.getid());
ps.setstring(2, user.getname());
ps.setdate(3, new java.sql.date(user.getbirthday().gettime()));
ps.setstring(4, user.gethobby());
ps.addbatch();
}
ps.executebatch();
} catch (exception e) {
e.printstacktrace();
} finally {
c3p0util.release(null, ps, conn);
}
}
以上就是xml文件怎么读取到数据库的详细内容。
