Parsing complex XML and inserting databases using XStream (i)

Source: Internet
Author: User
Tags one table stmt

Environment:

Springboot+mysql

I just want to say that JPA is really super-useful and ready to delve into it.

Import dependency:

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
The Lombok project is really handy for generating the ToString (), Setter (), Getter () method.
Intellij idea development needs to install Lombok plugin, while setting up Setting, Compiler, Annotation processors, Enable Annotation process ing tick. But I didn't check it.

Reference:http://www.cnblogs.com/holten/p/5729226.html
https://yq.aliyun.com/articles/59972
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.9</version>
</dependency>

successfully completed a demobased on the following link:
http://blog.csdn.net/yucaifu1989/article/details/26476835

XStream Note Description:
http://blog.csdn.net/gaozhlzh/article/details/6826140
Https://www.cnblogs.com/vmkash/p/5524809.html
JDBC Connection pool:
http://blog.csdn.net/emperor_xdy/article/details/6892607

a full range of examples:
https://www.cnblogs.com/XL-Liang/archive/2013/03/22/2974987.html

Other valuable references:
Https://www.cnblogs.com/johnsonwei/p/5778406.html
http://blog.csdn.net/rainbow_m/article/details/47783337
http://blog.csdn.net/yobsun/article/details/51890656
Https://www.cnblogs.com/zr520/archive/2016/04/06/5357459.html
http://blog.csdn.net/white_smile/article/details/43085491

I used hibernate to generate a table in my project, and I'm not sure how to use hibernate specifically. But it's a table problem. The JDBC Insert uses PREPAREDSTATEMENT,JPA to insert the first-post code, followed by my research.

There are two types of complex XML that I have seen before (no attribute-related studies):
(1) contains many sub-modules
The comparison is probably like this:
<?xml version= "1.0" encoding= "GBK" standalone= "no"?>
<Demo>
<A>
<a1></a1>
<a2></a2>
</A>
<B>
<b1></b1>
<b2></b2>
</B>
<C>
<D>
<cd1></cd1>
<cd2><cd2>
</D>
</C>
</Demo>
The examples in the following links perfectly solve the above situation:
http://blog.csdn.net/yucaifu1989/article/details/26476835
(2), segment repetition
What I'm encountering is this:

<?xml version= "1.0" encoding= "UTF-8"?>

<Data>

<Bean>

<A>a</A>

<B>b</B>

<C>1</C>

<A>a</A>

<B>b</B>

<C>2</C>

<D>

<F></F>

</D>

</Bean>

</Data>

Analysis:
Requests that the returned XML be parsed into a Java object and inserted into a database, dividing the contents of the XML into two tables to achieve a one-to-many relationship. Useful fields are a, B, C.
One table: A, B
More table: C

Method One:
Create three classes: Data (), Bean (), D ()
The Bean () class stores ABC and object d as collections or arrays.
JPA annotations @data can generate ToString (), setter (), Getter () methods
@Data
public class Bean {
Private list<string> A;
Private list<string> B;
Private list<string> C;
Private d D;
}
Here we have the class data (), then we can't use @data annotations, can write the Get (), set () method manually
public class Data {
Private Bean bean ;

Public Bean Getbean () {
return BEAN;
}

public void Setbean (bean bean) {
This. Bean = bean;
}
}
Class D omitted.
Write a converted class again
Public class Parsexmlutil {
/**
* Serialization of XML strings as objects
*
* @param XML XML string
*
* @return Channels Object
*/
Public static Data fromxml (String xml) {
/**
* New Domdriver () to resolve java.lang.noclassdeffounderror:org/xmlpull/v1/xmlpullparserfactory issues
*/

XStream XStream = new XStream (new Domdriver ());
xstream.processannotations (bean.class);
xstream.processannotations (data.class);
Xstream.alias ("DATA", data.class);
Xstream.alias ("BEAN", bean.class);
Xstream.alias ("A", string.class);
xstream.addimplicitcollection (Bean.class, "A");
Xstream.alias ("B", string.class);
xstream.addimplicitcollection (Bean.class, "B");
Xstream.alias ("C", string.class);
xstream.addimplicitcollection (Bean.class, "C");
Xstream.alias ("D", D.class);

Return (Data) xstream.fromxml (XML);
}
}
Parse Yes, now write for inserting the database:
Application.properties configuration file :
server.port=8088

#oracle
#driver =oracle.jdbc.driver.oracledriver
#url =JDBC:ORACLE:THIN:@10.95.18.124:1521:ORCL
#spring. Datasource.url=jdbc:oracle:thin: @localhost: 1521:ORCL
#username =root
#password =root

#mysql
Driver=com.mysql.jdbc.driver
Url=jdbc:mysql://localhost:3306/work
Username=root
Password=root

#配置JPA
#spring. jpa.database=oracle
Spring.jpa.database=mysql
Spring.jpa.show-sql=true
Spring.jpa.generate-ddl=true
I use MySQL.
Public class Connectionutils {
private static String URL;
private static String driver;
private static String username;
private static String password;
static{
Properties Props = new properties ();
try {
Read Database configuration information in//subordinate files
Props.load (ConnectionUtils.class.getClassLoader ()
. getResourceAsStream ("application.properties"));
} catch (IOException e) {
e.printstacktrace ();
}
if (props! = null) {
url = props.getproperty ("url");
Driver = props.getproperty ("Driver");
username = props.getproperty ("username");
Password = props.getproperty ("password");
//load and register database drivers
try {
Class.forName (driver);
} catch (ClassNotFoundException e) {
e.printstacktrace ();
}
}
}
Public static Connection getconnection () throws SQLException {
return drivermanager.getconnection (URL, username, password);
}

Public static void close (Connection con) {
try {
if (con! = null) {
con.close ();
}
} catch (SQLException e) {
e.printstacktrace ();
}
}

Public static void Close (PreparedStatement stmt) {
try {
if (stmt! = null) {
stmt.close ();
}
} catch (SQLException e) {
e.printstacktrace ();
}
}

Public static void Close (ResultSet rs) {
try {
if (rs! = null) {
rs.close ();
}
} catch (SQLException e) {
e.printstacktrace ();
}
}

}

Test:
Public class Test {
Public static void Main (string[] args) {

String sqlone = "INSERT into one (a, b)" +
"VALUES (?,?);";


String sqlmore = "INSERT into more (one_id,c)" +
"VALUES (?,?);";

Connection conn = null;
PreparedStatement pstmt = null;
PreparedStatement PST = NULL;

try {
conn = Connectionutils.getconnection ();
pstmt = (preparedstatement) conn.preparestatement (sqlone);
String code = "<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >\n" +
"\ n" +
"<DATA> \ n" +
"<BEAN> \ n" +
"<A>2018-01-22</A> \ n" +
"<B> Taiyuan </B> \ n" +
"<c>1</c>\n" +

"<A>2018-01-22</A> \ n" +
"<B> Taiyuan </B> \ n" +
"<c>2</c>\n" +
"<D><F>enen</F></D>" +
  "</BEAN> \ n" + 
"&L T;/data>\n ";
Data data = (data) parsexmlutil.fromxml (code);
Bean bean = Data.getbean ();
Pstmt.setstring (1, Bean.geta (). Get (0));
Pstmt.setstring (2, Bean.getb (). Get (0));

for (int i = 0; i < Bean.geta (). Size (); i++) {
PST = (preparedstatement) conn.pr Eparestatement (Sqlmore);
Pstmt.setstring (1, Bean.getc (). get (i));
Pstmt.addbatch ();
}
Pstmt.executebatch ();//Bulk Increase

System.out.println ("Import XML to database successfully! ");
} catch (Exception e) {
E.printstacktrace ();
} finally {
Connectionutils.close (PST);
Connectionutils.close (PSTMT);
Connectionutils.close (conn);
}
}
}
This is for only one bean, and we are parsing multiple beans in the project.
Transfer to (ii):


Parsing complex XML and inserting databases using XStream (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.