json转xml

1、引入jar包



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.1.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            net.sf.json-lib
            json-lib
            2.3
            jdk15
        
        
            com.thoughtworks.xstream
            xstream
            1.4.10
        
        
        
            commons-beanutils
            commons-beanutils
            1.8.3
        
        
        
            commons-collections
            commons-collections
            3.2
        
        
        
            org.apache.commons
            commons-io
            1.3.2
        
        
        
            commons-lang
            commons-lang
            2.6
        
        
        
            net.sf.ezmorph
            ezmorph
            1.0.6
        
        
        
            org.json
            json
            20160810
        
        
            de.odysseus.staxon
            staxon
            1.2
        
        
            xom
            xom
            1.1
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2、代码:
 

package com.example.demo.tree;

import com.thoughtworks.xstream.XStream;
import de.odysseus.staxon.json.JsonXMLConfig;
import de.odysseus.staxon.json.JsonXMLConfigBuilder;
import de.odysseus.staxon.json.JsonXMLInputFactory;
import de.odysseus.staxon.xml.util.PrettyXMLEventWriter;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import java.io.StringReader;
import java.io.StringWriter;

public class XmlTest {

    public static String textMessageToXml(TreeNode mm) {

        XStream xStream = new XStream();

        xStream.alias("xml", TreeNode.class);

        return xStream.toXML(mm);

    }

    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 (Exception ex){

            }
        }
        System.out.println("".length());
        String result=output.toString();
        if(result.length()>=38){
                result=result.substring(38);
        }
        return result;
    }


    public static void main(String[] args) {
        String json = "{\n" +
                "  \"ksnos\": [\n" +
                "    {\n" +
                "      \"ksno\": \"1002103\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"ksno\": \"1002104\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"ksno\": \"17410101152304\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"count\": \"3\"\n" +
                "}";
      String xml=json2xml(json);
        System.out.println(xml);
    }


}

 

你可能感兴趣的:(java)