Convert json string to xml file format

    1 Method one is as follows, source: http://blog.csdn.net/wzygis/article/details/46739525
         <dependency>
            <groupId>de.odysseus.staxon</groupId>
            <artifactId>staxon</artifactId>
            <version>1.2</version>
        </dependency>
  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 ();
            }
        }

 

   2 Of course, many online use:

XMLSerializer serializer = new XMLSerializer();  
            JSON jsonObject = JSONSerializer.toJSON(json);  
            return serializer.write(jsonObject);  

   Converted in this way, the required dependencies are:

<dependency>  
    <groupId>net.sf.json-lib</groupId>  
    <artifactId>json-lib</artifactId>  
    <version>2.1</version>  
    <classifier>jdk15</classifier>  
</dependency>  
<dependency>  
    <groupId>xom</groupId>  
    <artifactId>xom</artifactId>  
    <version>1.2.5</version>  
    <type>jar</type>  
    <scope>compile</scope>  
</dependency>  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326313492&siteId=291194637