使用XStream解析复杂XML并插入数据库

Posted psyche61

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用XStream解析复杂XML并插入数据库相关的知识,希望对你有一定的参考价值。

 环境:

Springboot+mysql

我只想说jpa真的超级好用,准备深入研究一下~

导入依赖:

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
lombok项目对于生成ToString(),Setter(),Getter()方法真的是太方便了。
Intellij idea开发的话需要安装Lombok plugin,同时设置 Setting -> Compiler -> Annotation Processors -> Enable annotation processing勾选。不过我没有勾选。

参考:
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>

根据下列链接成功完成一个Demo
  http://blog.csdn.net/yucaifu1989/article/details/26476835

XStream注解说明:
http://blog.csdn.net/gaozhlzh/article/details/6826140
https://www.cnblogs.com/vmkash/p/5524809.html
jdbc连接池:
http://blog.csdn.net/emperor_xdy/article/details/6892607

很全的各种举例:
https://www.cnblogs.com/XL-Liang/archive/2013/03/22/2974987.html

其余有价值参考:
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

项目中我使用了Hibernate去生成表,Hibernate具体使用我还不太会。不过也就是表问题。jdbc插入使用PreparedStatement,jpa插入先贴代码,后续我再研究研究。

复杂XML目前我见过两种(不涉及属性的研究):
(1)包含很多子模块
比较有代表性大概这样的:
<?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>
以下链接中的例子完美解决了上述情况:
 http://blog.csdn.net/yucaifu1989/article/details/26476835
(2)、成段重复
我遇到的是这样的情况:

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

分析:
要求将返回来的xml解析成java对象并插入数据库,将XML中的内容分成两个表,实现一对多的关系。有用的字段为A、B、C。
one表:A、B
more表:C

方法一:
创建三个类:分别是Data(),Bean(),D()
Bean()类中以集合或者数组存储ABC和对象D。
//jpa注解@Data可以生成tostring(),setter(),getter()方法
@Data
public class Bean {
private List<String> A;
private List<String> B;
private List<String> C;
   private D d;
}
这里我们有类Data(),那么就不能使用@Data注解了,可以手动写get()、set()方法
public class Data {
private Bean BEAN;

public Bean getBean() {
return BEAN;
}

public void setBean(Bean BEAN) {
this.BEAN = BEAN;
}
}
D类省略。
再写一个转换的类
public class ParseXmlUtil {
/**
* 序列化XML字符串为对象
*
* @param xml xml字符串
*
* @return Channels对象
*/
public static Data fromXML(String xml) {
/**
* new DomDriver()用于解决java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserFactory问题
*/

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);
}
}
解析可以了,现在写用于插入数据库的:
application.properties配置文件
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
我使用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 {
//从属性文件中读取数据库配置信息
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");
//装载并注册数据库驱动
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();
}
}

}

测试:
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>太原</B>   \\n" +
"<C>1</C>\\n" +

" <A>2018-01-22</A>   \\n" +
" <B>太原</B>   \\n" +
"<C>2</C>\\n" +
            "<D><F>enen</F></D>"+
                    "  </BEAN> \\n" +
"</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.prepareStatement(sqlMore);
pstmt.setString(1, bean.getC().get(i));
pstmt.addBatch();
}
pstmt.executeBatch();//批量增加

System.out.println("将xml导入数据库成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionUtils.close(pst);
ConnectionUtils.close(pstmt);
ConnectionUtils.close(conn);
}
}
}
这是针对只有一个Bean,项目中我们是多条Bean进行解析。
传送至(二):


以上是关于使用XStream解析复杂XML并插入数据库的主要内容,如果未能解决你的问题,请参考以下文章

XStream解析xml代码

XStream解析XML文本并用反射机制转换为对象

XStream xml 解析框架使用笔记

微信公众号xml数据解析之xstream

Xstream 解析xml文件内容

使用XStream是实现XML与Java对象的转换--Object Stream