staxon实现json和xml互转

原文链接

pom.xml:

1
2
3
4
5
< dependency >
     < groupId >de.odysseus.staxon</ groupId >
     < artifactId >staxon</ artifactId >
     < version >1.3</ version >
</ dependency >

转换工具类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package  com.nihaorz.utils;
 
import  java.io.IOException;
import  java.io.StringReader;
import  java.io.StringWriter;
 
import  javax.xml.stream.XMLEventReader;
import  javax.xml.stream.XMLEventWriter;
import  javax.xml.stream.XMLInputFactory;
import  javax.xml.stream.XMLOutputFactory;
 
import  de.odysseus.staxon.json.JsonXMLConfig;
import  de.odysseus.staxon.json.JsonXMLConfigBuilder;
import  de.odysseus.staxon.json.JsonXMLInputFactory;
import  de.odysseus.staxon.json.JsonXMLOutputFactory;
import  de.odysseus.staxon.xml.util.PrettyXMLEventWriter;
 
public  class  StaxonUtils {
     /**
      * json string convert to xml string
      */
     public  static  String json2xml(String json) {
         StringReader input =  new  StringReader(json);
         StringWriter output =  new  StringWriter();
         JsonXMLConfig config =  new  JsonXMLConfigBuilder().multiplePI( false )
                 .repairingNamespaces( false ).build();
         try  {
             XMLEventReader reader =  new  JsonXMLInputFactory(config)
                     .createXMLEventReader(input);
             XMLEventWriter writer = XMLOutputFactory.newInstance()
                     .createXMLEventWriter(output);
             writer =  new  PrettyXMLEventWriter(writer);
             writer.add(reader);
             reader.close();
             writer.close();
         catch  (Exception e) {
             e.printStackTrace();
         finally  {
             try  {
                 output.close();
                 input.close();
             catch  (IOException e) {
                 e.printStackTrace();
             }
         }
         if  (output.toString().length() >=  38 ) {
             // remove <?xml version="1.0" encoding="UTF-8"?>
             return  output.toString().substring( 39 );
         }
         return  output.toString();
     }
 
     /**
      * xml string convert to json string
      */
     public  static  String xml2json(String xml) {
         StringReader input =  new  StringReader(xml);
         StringWriter output =  new  StringWriter();
         JsonXMLConfig config =  new  JsonXMLConfigBuilder().autoArray( true )
                 .autoPrimitive( true ).prettyPrint( true ).build();
         try  {
             XMLEventReader reader = XMLInputFactory.newInstance()
                     .createXMLEventReader(input);
             XMLEventWriter writer =  new  JsonXMLOutputFactory(config)
                     .createXMLEventWriter(output);
             writer.add(reader);
             reader.close();
             writer.close();
         catch  (Exception e) {
             e.printStackTrace();
         finally  {
             try  {
                 output.close();
                 input.close();
             catch  (IOException e) {
                 e.printStackTrace();
             }
         }
         return  output.toString();
     }
}

测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package  com.nihaorz.xmltojson;
 
import  org.junit.Test;
 
import  com.nihaorz.utils.StaxonUtils;
 
public  class  UtilsTest {
     
     @Test
     public  void  test_xmltojson(){
         String xml =  "<goods><name type=\"book\" prices=\"100\">钢铁是怎样炼成的</name><name1 type='book' prices='100'>钢铁是怎样炼成的</name1><name type='book' prices='100'>钢铁是怎样炼成的</name></goods>" ;
         System.out.println(xml);
         String json = StaxonUtils.xml2json(xml); 
         System.out.println(json);
     }
     
     @Test
     public  void  test_jsontoxml(){
         String json =  "{\"goods\":{\"name\":{\"@prices\":100,\"@type\":\"book\",\"$\":\"钢铁是怎样炼成的\"},\"name1\":{\"@prices\":\"100\",\"@type\":\"book\",\"$\":\"钢铁是怎样炼成的\"},\"name\":{\"@prices\":\"100\",\"@type\":\"book\",\"$\":\"钢铁是怎样炼成的\"}}}" ;
         System.out.println(json);
         String xml = StaxonUtils.json2xml(json);
         System.out.println(xml);
     }
     
     
}

猜你喜欢

转载自blog.csdn.net/cinderella___/article/details/80531575
今日推荐