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

    1方法一如下,来源: http://blog.csdn.net/wzygis/article/details/46739525
         
            de.odysseus.staxon
            staxon
            1.2
        
  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);  

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

  
    net.sf.json-lib  
    json-lib  
    2.1  
    jdk15  
  
  
    xom  
    xom  
    1.2.5  
    jar  
    compile  
  

 

你可能感兴趣的:(java编程,xml,2,json)