依赖包

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
View Code
xml转json
1 XmlMapper xmlMapper = new XmlMapper();
2 //对象转xml
3 User user = new User("吴建波", 31, "北京市昌平区域沙河在南一村", "118", "男", "168");
4
5 String temp = xmlMapper.writeValueAsString(user);
6 System.out.println(temp);
7
8 // xml转对象转json
9 String temp2 = "<User><name>吴建波</name><age>31</age><adderss>北京市昌平区域沙河在南一村</adderss><weight>118</weight><sex>男</sex><height>168</height></User>";
10 User userObject = xmlMapper.readValue(temp2, User.class);
11 System.out.println(userObject);
12 System.out.println(JSON.toJSONString(userObject));
13 // xml转Map转Json
14 String temp3 = "<apps><app><id>1</id><name>Google Maps</name><virsion>1.0</virsion></app></apps>";
15 temp3 = "<appsii>" + temp3 + "</appsii>";
16 Map map = xmlMapper.readValue(temp3, HashMap.class);
17 System.out.println(map);
18 System.out.println(JSON.toJSONString(map));
对象转xml