json字符串转换成xml文件格式

    1方法一如下,来源: 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 当然网上很多都是用:

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

   这种方式转换的,需要的依赖:

<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>  

猜你喜欢

转载自java-holding.iteye.com/blog/2365464