1.pom依赖
<!-- 方法2的依赖-->
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>jackson-xml-databind</artifactId>
<version>0.6.2</version>
</dependency>
<!-- 方法1的依赖-->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
</dependency>
2.要读取的xml内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.sharejdbctable.mapper.UserMapper">
<insert id="save" parameterType="com.example.sharejdbctable.po.User">
INSERT INTO t_user(id,name,sex)
VALUES
(
#{id},#{name},#{sex}
)
</insert>
<select id="get" parameterType="long" resultType="com.example.sharejdbctable.po.User">
select * from t_user where id = #{id}
</select>
<insert id="saveOrder" parameterType="com.example.sharejdbctable.dto.OrderDto">
INSERT INTO t_order(order_id,user_id,status)
VALUES
(
#{order_id},#{user_id},#{status}
)
</insert>
<insert id="saveOrderDetail" parameterType="java.util.ArrayList">
INSERT INTO t_order_item(order_item_id,order_id,user_id,status)
VALUES
<foreach collection="list" item="item" separator=",">
(#{item.order_item_id},#{item.order_id},#{item.user_id},#{item.status})
</foreach>
</insert>
</mapper>
3.转json代码
StringWriter writer = null;
try {
//文档构建工厂
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
//文档构建器
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
//读取xml文档转为对象
Document doc = dBuilder.parse(new File("D:/other/sharejdbc/sharejdbctable/src/main/resources/mapper/mapper.xml"));
//创建dom节点的输入源,该操作将应用于以此节点为根的子树
DOMSource domSource = new DOMSource(doc);
writer = new StringWriter();
//创建流对象
StreamResult result = new StreamResult(writer);
//构建TransformerFactory工厂
TransformerFactory tf = TransformerFactory.newInstance();
//Transformer可以将源树转为结果树
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
//方法一:完美解决
JSONObject jsonObject1 = XML.toJSONObject(writer.toString());
System.out.println(jsonObject1);
System.out.println("--------------------------");
//方法二:有缺陷,无法得到完整的xml内容
ObjectMapper xmlMapper = new XmlMapper();
Object jsonObject = xmlMapper.readValue(writer.toString(), Object.class);
System.out.println(JSONUtils.toJSONString(jsonObject));
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
4.控制台输出如下:可见方法一是完整输出,但是方法二无法完成
{“mapper”:{“select”:{“parameterType”:“long”,“id”:“get”,“resultType”:“com.example.sharejdbctable.po.User”,“content”:“select * from t_user where id = #{id}”},“namespace”:“com.example.sharejdbctable.mapper.UserMapper”,“insert”:[{“parameterType”:“com.example.sharejdbctable.po.User”,“id”:“save”,“content”:“INSERT INTO t_user(id,name,sex)\r\n VALUES\r\n (\r\n #{id},#{name},#{sex}\r\n )”},{“parameterType”:“com.example.sharejdbctable.dto.OrderDto”,“id”:“saveOrder”,“content”:“INSERT INTO t_order(order_id,user_id,status)\r\n VALUES\r\n (\r\n #{order_id},#{user_id},#{status}\r\n )”},{“parameterType”:“java.util.ArrayList”,“foreach”:{“item”:“item”,“collection”:“list”,“separator”:",",“content”:"(#{item.order_item_id},#{item.order_id},#{item.user_id},#{item.status})"},“id”:“saveOrderDetail”,“content”:“INSERT INTO t_order_item(order_item_id,order_id,user_id,status)\r\n VALUES”}]}}
{“namespace”:“com.example.sharejdbctable.mapper.UserMapper”,“insert”:{“id”:“saveOrderDetail”,“parameterType”:“java.util.ArrayList”,“foreach”:{“collection”:“list”,“item”:“item”,“separator”:",","":"\n\t\t(#{item.order_item_id},#{item.order_id},#{item.user_id},#{item.status})\n\t\t"}},“select”:{“id”:“get”,“parameterType”:“long”,“resultType”:“com.example.sharejdbctable.po.User”,"":"\n select * from t_user where id = #{id}\n "}}
方法一:
方法二: