依賴包

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58</version> </dependency>
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