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 list
##The sixth step, list
##The first step is to guide the package
c3p0
,dom4j,jaxen, MySQL-connector
Second stepxml file,configFile##xmlFile,
configFileDetailed 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
javabeanDo it according to the needs
Step 4
Tool class for c3p0package 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 list
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
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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools