Home  >  Article  >  Java  >  How to read xml file into database

How to read xml file into database

巴扎黑
巴扎黑Original
2017-06-26 11:30:112459browse

xmlRead the file into the database

The first step is to import package

c3p0,dom4j,jaxen,MySQL-connector

Second step xml file, configFile

The third stepjavabean

The fourth step c3p0Tool class

The fifth step Read xml file SAXReader# The xpath method in

## first requires the map collection Add an alias, traverse the read files,

gives liste1580111763df721c6bab76afcde5ef7

##The sixth step, liste1580111763df721c6bab76afcde5ef7gives c3p0 the class to connect to the database

##The first step is to guide the package

c3p0

,

dom4j,jaxen, MySQL-connector

Second step

xml file,configFile##xmlFile,

config

FileDetailed implementation according to requirementsXsdConstraints

<?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>

The third step

javabean

Do it according to the needs

Step 4

Tool class for 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;
    }
}
}

Step 5

Read the in the xml file SAXReader xpath wayFirst you need to add aliases to the

map

collection and traverse the read files, ##gave liste1580111763df721c6bab76afcde5ef7

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

The sixth step,

liste1580111763df721c6bab76afcde5ef7

gives

c3p0 the class to connect to the database

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

The above is the detailed content of How to read xml file into database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn